Routing

A central task solved by Pagekit is Routing. When the browser hits a URL, the framework determines, what action will be called.

Controller

The most common way of creating routes in Pagekit is to define a controller. A controller is responsible for handling requests, setting routes and rendering views.

Register a controller

You can register a controller inside your module configuration. Use the routes property to mount controllers to a route.

  1. 'routes' => [
  2.  
  3. '/hello' => [
  4. 'name' => '@hello/admin',
  5. 'controller' => [
  6. 'Pagekit\\Hello\\Controller\\HelloController'
  7. ]
  8. ]
  9.  
  10. ],

Basic structure

The class is annotated with @Route("/hello"), causing the controller to be mounted at http://example.com/hello/. That means it will respond to all requests to that URL and sub-URLs like http://example.com/hello/settings.

  1. namespace Pagekit\Hello\Controller;
  2.  
  3. /**
  4. * @Route("/hello")
  5. */
  6. class HelloController
  7. {
  8.  
  9. public function indexAction()
  10. {
  11. // ...
  12. }
  13.  
  14. public function settingsAction()
  15. {
  16. // ...
  17. }
  18. }

By default, your extension (or theme) will be booted and a set of default routes will be generated automatically. You can use the developer toolbar to view the newly registered routes (along with all core routes).

Here is how to understand a route:

RouteExampleDescription
Name@hello/hello/settingsThe name of the route can be used to generate URLs (has to be unique).
URI/hello/settingsThe path to access this route in the browser.
ActionPagekit\Hello\Controller\DefaultController::settingsActionThe controller action that will be called.

By default, routes will be of the form http://example.com/<extension>/<controller>/<action&gt;. A special action is indexAction, which will not be mounted at …/index, but at …/. Advanced options for custom routes are available of course, as you will see in the next sections.

Note If a route is not unique across your application, the one that has been added first is the one being used. As this is framework internal behavior that might change, you should not rely on this but rather make sure your routes are unique.

Annotations

A lot of the controller's behavior is determined by information annotated to the class and methods. Here is a quick overview, a detailed description for each annotation follows below.

AnnotationDescription
@RouteRoute to mount an action or the whole controller.
@RequestHandle parameter passing from the http request to the method.
@AccessCheck for user permissions.

Note Annotations will only work if you start the multiline comment with two asterisks, not just with one.

  1. // will NOT work:
  2. /* @Route("/hello") */
  3. // will work:
  4. /** @Route("/hello") */
  5. // will work:
  6. /**
  7. * @Route("/hello")
  8. */

@Route

Define the route the controller (or controller action) will be mounted at. It can be annotated to class and method definitions.

By default, a method called greetAction will be mounted as /greet under the route of the class. To add custom routes, you can add any number of additional routes to a method. Routes can also include dynamic parameters, which will be passed to the method.

  1. /**
  2. * @Route("/greet", name="@hello/greet/world")
  3. * @Route("/greet/{name}", name="@hello/greet/name")
  4. */
  5. public function greetAction($name = 'World')
  6. {
  7. // ...
  8. }

Parameters can be specified to fulfill certain requirements (for example limit the value to numbers). You can name a route so that you can reference from your code. Use PHP argument defaults at the method definition to make a parameter optional.

Routes can be bound to certain HTTP-methods (e.g. GET or POST). This is especially useful for RESTful APIs.

  1. /**
  2. * @Route("/view/{id}", name="@hello/view/id", requirements={"id"="\d+"}, methods="GET")
  3. */
  4. public function viewAction($id = 0)
  5. {
  6. // ...
  7. }

Note For detailed information and more examples, have a look at Symfony's own documentation on the @Route annotation.

@Request

You can specify the types of data passed via a request and match them to parameters passed to the annotated method.

The array maps name to type. name is the key inside the request data. type can be int, string, array and advanced types like int[] for an array of integers. If no type is specified, string is assumed by default.

The order of the keys will define the order, in which parameters are being passed to the method. The parameter name in the method head can be anything.

  1. /**
  2. * @Request({"id": "int", "title", "config": "array"}, csrf=true)
  3. */
  4. public function saveAction($id, $title, $config)
  5. {
  6. // ...
  7. }

You can also check for a token to protect against CSRF. Add csrf=true to your request annotation and include the @token call in the view that submits a form to this method.

Check out Pagekit\Filter\FilterManager for a complete list of available filters. Some filters have additional options, like pregreplace.

  1. /**
  2. * @Request({"folders":"pregreplace[]"}, options={"folders" = {"pattern":"/[^a-z0-9_-]/i"}})
  3. */
  4. public function deleteFolders($folders)
  5. {
  6. // ...
  7. }

@Access

You can specify certain user permissions required to access a specific method or the whole controller.

Controllers should always be specific to the frontend or the admin panel. So far, we have seen controllers for the frontend. An administration controller will only be accessible for users with the Access admin area permission. Also, all routes for that controller will have a leading admin/ in the URL. As a result, views will also render in the admin layout and not in the default theme layout.

  1. /**
  2. * @Access(admin=true)
  3. */
  4. class SettingsController
  5. {
  6. // ...
  7. }

Now, only users with the Access admin area permission can access the controller actions. If you want to use further restrictions and only allow certain users to do specific actions (like manage users etc.), you can add restrictions to single controller actions.

Define permissions in the extension.php (or theme.php) file and combine them however you want. Access restrictions from the controller level will be combined with access restrictions on the single actions. Therefore you can set a basic minimum access level for your controller and limit certain actions, like administrative actions, to users with more specific permissions.

  1. /**
  2. * @Access("hello: manage users")
  3. */
  4. public function saveAction()
  5. {
  6. // ...
  7. }

Of course, you can also use these restrictions even if the controller is not an admin area controller. You can also check for admin permissions on single controller actions.

  1. /**
  2. * @Access("hello: edit article", admin=true)
  3. */
  4. public function editAction()
  5. {
  6. // ...
  7. }

Generating URLs

Using the URL service, you can generate URLs to your routes.

  1. $this['url']->route('@hello/default/index') // '/hello/default/index'
  2. $this['url']->route('@hello/default/index', true) // 'http://example.com/hello/default/index'
  3. $this['url']->route('@hello/view/id', ['id' => 23]) // '/hello/view/23'

Pagekit's routes can be described by an internal route syntax. They consist of the route name (e.g. '@hello/name'), optionally followed by GET parameters (e.g. '@hello/name?name=World'). This is called a link. It separates the actual route URI from the route itself.