Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
This will match:
$url = Router::match(array('controller' => 'users', 'action' => 'login'));
returns /login
For URLs templates with no insert parameters (i.e. elements like {:id} that are replaced
with a value), all parameters must match exactly as they appear in the route parameters.
Alternatively to using a full array, you can specify routes using a more compact syntax. The
above example can be written as:
$url = Router::match('Users::login'); // still returns /login
You can combine this with more complicated routes; for example:
Router::connect('/posts/{:id:\d+}', array('controller' => 'posts', 'action' => 'view'));
This will match:
$url = Router::match(array('controller' => 'posts', 'action' => 'view', 'id' => '1138'));
returns /posts/1138
Again, you can specify the same URL with a more compact syntax, as in the following:
$url = Router::match(array('Posts::view', 'id' => '1138'));
again, returns /posts/1138
You can use either syntax anywhere an URL is accepted, i.e. when redirecting
or creating links using the Html helper.
public static match ( array | string $url = [], |
||
$url | array | string | An array of parameters to match, or paremeters in their shorthand form (i.e. `'Posts::view'`). Also takes non-routed, manually generated URL strings. |
$context | This supplies the context for any persistent parameters, as well as the base URL for the application. | |
$options | array | Options for the generation of the matched URL. Currently accepted values are: - `'absolute'` _boolean_: Indicates whether or not the returned URL should be an absolute path (i.e. including scheme and host name). - `'host'` _string_: If `'absolute'` is `true`, sets the host name to be used, or overrides the one provided in `$context`. - `'scheme'` _string_: If `'absolute'` is `true`, sets the URL scheme to be used, or overrides the one provided in `$context`. - `'scope'` _string_: Optionnal scope name. |
Résultat | string | Returns a generated URL, based on the URL template of the matched route, and prefixed with the base URL of the application. |