Router #git

Froq! applies a simple yet fast routing way to resolve routes and call related / appropriate methods or callables (for micro), and beside, assembles all route definitions into one PHP file named app/config/routes.php as default.

What to know (about definitions)?

  • · No Controller or Action suffix is needed, so you give controller and action name only.
  • · No action name is needed for index() action, so you give controller name only.
  • · For naming the call path, controller and action name must be separated by dot (.) but nothing else.
  • · No HTTP method is needed if all methods are allowed for that action, so you give action name only (or asterisk (*) for the sake of readability if you wish).
  • · Accessing to an action can be constrained by a single HTTP method or multiple HTTP methods using this name or these names, and multiple methods must be separated comma (,) but nothing else.
  • · All :foo param will be converted to (?<foo>[^/]+) in RegExp patterns, but beside, you can also use raw RegExp patterns as well.
  • · All RegExp patterns take their final form internally, and all ends with /? suffix by default, and beside, all URL paths are normallised reducing slashes (/) before compilation and the compiled final / whole pattern is case-sensitive (i modifier not used).
  • · You can manipulate action names by placing path params in call paths.


Not: Consider that all the examples below are in the main array that returns from app/config/routes.php file.

Definition conventions

// Assoc arrays (a key/value).
'route' => 'Call.path',
'route' => ['METHOD' => 'Call.path'],

// List arrays (a two-dim).
['route', 'Call.path'],
['route' => ['METHOD' => 'Call.path']],

Simple definitions

// For HomeController::index().
'/' => 'Home',

// For PostController::index().
'/post' => 'Post',

// For PostController::showAction().
'/post/:id' => 'Post.show',

// And PostController class.
public function showAction($id) { ... }

// For PostController::showAction(), slug is optional.
'/post/:id(/:slug)?' => 'Post.show',

// And PostController class ("null" is just for "?" above).
public function showAction($id, $slug = null) { ... }

Constrain action calls with HTTP methods

// For PostController::showAction().
'/post/:id' => ['GET' => 'Post.show'],
// For PostController::removeAction().
'/post/:id' => ['DELETE' => 'Post.remove'],
// For PostController::updateAction().
'/post/:id' => ['PUT,PATCH' => 'Post.update'],

// Or all-in-one directive.
'/post/:id' => ['GET' => 'Book.show',
                'DELETE' => 'Post.remove',
                'PUT,PATCH' => 'Book.update'],

// Or catch all methods.
'/post' => 'Post',
'/post' => ['*' => 'Post'],

Note: If an action is constrained with HTTP method / methods and incoming request method doesn't match, an froq\AppException will be thrown (with code: 405, cause: froq\http\exception\client\NotAllowedException). These type of errors can be handled in error() method.

Manipulate action names

// For UserController::loginAction()/logoutAction().
'/user/:x{login|logout}' => 'User.{x}',

// For UserController::lostPasswordAction()/resetPasswordAction()/renewPasswordAction().
'/user/password/:x{lost|reset|renew}' => 'User.{x}Password',

Raw RegExp patterns

// Constrain id with digits.
'/post/:id[\d]+' => 'Post.show',
'/post/:id{\d+}' => 'Post.show',
'/post/(?<id>\d+)' => 'Post.show',

// Add slug as an optional path param.
'/post/(?<id>\d+)(/(?<slug>[^/]+))?' => 'Post.show',
Getting Started Installation Configuration Web Servers
Application & Components Application Controller Repository View Routing
HTTP & Components Request Response Payloads
Database & Components Database Queries & Results Transactions Entities
Utilities Sugars Sugar Classes Sugar Functions Sugar Constants
Froq! Framework · Home · Docs · API · GitHub