cakephpcakephp-2.3cakephp-routing

router::connect to forward named params in CakePHP


http://example.com/foo/c:1

How can I write working router::connect to send such links to

controller=listings
action=search
named array(cat=1) the id I do not know, it's a dynamic url

Please take note, that in the URL it is "c" but I need to forward it as "cat".


Solution

  • You can specify the routing as follow in the Config/routes.php.

    ```

    Router::connect(
            '/:query/*', array('controller' => 'listings', 'action' => 'search'), array(
                'params' => array('query', 'cat'),
                'named' => array(
                    'query', 'cat'
                )
            )
        );
    

    ```

    and specify the link as

    ```

    echo $this->Html->link('text goes here', array('controller' => 'listings', 'action' => 'search', 'query' => 'foo', 'cat' => 1));
    

    ```