Skip to content
Grav 2.0 is officially stable. Read the announcement →
General

Custom controller for a certain route/path

Solved by pamtbaau View solution

Started by Francesco Quagliati 6 years ago · 3 replies · 1021 views
6 years ago

Hi all!
Is it possible to have a custom route with a custom controller for a certain page?
I mean, if i have a simple website but i've a certain page of the website that needs some PHP or something more complex than what i can achieve with "plain" grav page, is there the possiblity to do so?
Like a page that use some web service to do something, or to programmatically redirect people to somewhere given some conditions and so on.
I know that, even if that's possible, then the menu integration is different theme to theme and i've to handle it case by case differently.
Thanks!

6 years ago Solution

@FrancescoQ, As promised...

Grav allows plugins and themes to subscribe to events and execute custom php code to handle the event. During these events, users can be redirected, page content can be changed, etc. You name it...

Have a look at the docs about plugin basics, a tutorial, event hooks, and the Grav event lifecycle: https://learn.getgrav.org/plugins.

Here are the steps to create a new plugin:

  • Install the devtools plugin: $ bin/gpm install devtools
  • Create a new plugin using a wizard: $ bin/plugin devtools new-plugin

The newly created plugin subscribes to events and adds some text to the top of every page. Not very handy, but it gives you an idea of the skeleton of a plugin.

Once you have taken a look at the docs and the newly created plugin, and have some specific questions, please ask on the forum.

6 years ago

If you want an example of this, the Scholar-theme uses a custom Router to handle special-purpose routes affixed to the URL.

As pamtbaau says, you can intercept and manipulate any and all processing done by Grav in extensions.

6 years ago

Wow, thanks both! exactly what i was searching for! (i was debugging the admin plugin because i tought that it could do something similar to handle all the admin routes... )
That's great!
Thankyou

Suggested topics

Topic Participants Replies Views Activity
General · by Hanns Mattes, 8 hours ago
1 2 10 minutes ago
General · by Andy Miller, 26 minutes ago
0 0 26 minutes ago
General · by Jerry Hunt, 4 days ago
2 136 1 day ago
General · by pamtbaau, 1 day ago
1 93 1 day ago
General · by Andy Miller, 2 days ago
0 77 2 days ago