Routing is the main part to any application. Tomahawk makes use of Symfony's poweful Routing Component, as there's no point re-inventing the wheel. You can read more about Symfony Routing here.

Defining Routes

You add routes in the app/Resources/routes.php file.

You define routes using the Tomahawk\Routing\Router class. There should be one instantiated in this file already.

From the example above, the 1st parameter is the path, the 2nd is the name of the route (used by the URL Generator) and the 3rd is the callback.

A callback can be either a closure or string defining the class and method in the following format: Controller::action

Route Parameters

You can define parameters to capture in the URL.

You can also set a default value:

Declaring parameter constraints:

You can also have the Request passed in to the callback:

Route Groups

You can also define a group to control a set of routes. The first parameter to the group method can either be a string or array of settings.

When a string is passed this should be the prefix for the route group:

An array can be passed with the following options available:

  • prefix - string - Prefix for the route group
  • domain - string - Domain host pattern. e.g You can also set placeholders. e.g {subdomain}
  • schemes - string|array - What schemes a route should match (http, https)
  • methods - string|array - What methods a route should match (GET, POST etc)
  • defaults - array - Default values for parameters. e.g. ['version', 1]. Can also be used to pass information to the Requests attributes
  • options - array - Options for route(s) Will get passed to the request
  • requirements - array - Requirements for route parameters. e.g. ['id', "\d+"]

Http and Https Routes

TomahawkPHP has 2 route helpers to make setting that a route requires http or https with the requiresHttp and the requiresHttps methods: