Registering routes and controllers through plugins

I see I can register routes and controllers through plugins using the $kirby->set('route') command.

Can I get how the route thing would work? what should I put in the file that it points to?
Same question for the controller - do I just put a class inside of it?

Thanks!

You can read more about routing in the docs. A route is always virtual, so the file that the route points to must actually not exist for it to work. If you use routing, you don’t need a controller for the route.

So the $kirby->set() for a route is an alias for the c::set() ?

So how does the controller work when I define it with $kirby->set('controller') ? is it available everywhere? kind of like the routing in the panel?

array(
  'pattern' => 'login',
  'action'  => 'AuthController::login',
  'filter'  => 'isInstalled',
  'method'  => 'GET|POST'
)

if I define a controller than I will be able to use it as an action in a route?

Kind of, yes. There is nothing special about the $kirby->set() way of defining routes other than that it can be used inside plugins (while c::set() can’t be).

Just as with routes: A controller that is defined using $kirby->set() works just the same as a controller you place in the site/controllers directory.

Controllers don’t have anything to do with routing, they are used for templates. What you mean is defining a route action as a controller class method. Such a class can be defined by you with standard PHP code. Just make sure that the class is loaded or autoloadable.

1 Like

ok, so using the set for controllers will basically mean that if I have a page with the correct filename this controller will work… how does kirby handle clashes in this case? (let’s say a plugin defines a controller, but there is also a controller with similar name in the controllers directory)

Thanks for the help!

A controller in the controllers directory will always override the ones from plugins. :slight_smile:

1 Like