Search code examples
cakephproutesslug

How can I get the connected route from a URL string in CakePHP?


CakePHP appears to have a function to translate a requested URL and determine what controller and action to perform, seeing this must be performed with each http request.

Is there a way I can utilize this process within a controller or elsewhere in the system? The best outcome would be to have a function where I input a URL string, and the response is an array with controller details. eg:

$url_route = RouteFunction('/page/url/here');

// $url_route = array(
//    'controller' => 'page', 
//    'action' => 'display', 
//    'pass' => array('url', 'here')
// );

Solution

  • For this you can use Router::parse().

    For example:

    $route = Router::parse('/users/view/21');
    debug($route);
    

    will by default output:

    array(
        'controller' => 'users',
        'action' => 'view',
        'named' => array(),
        'pass' => array(
                (int) 0 => '21'
        ),
        'plugin' => null
    )