phpcakephpcakephp-2.4cakephp-routing

Pass Query string to cake style URL in cakephp and give ext of HTML


What I am trying to achieve:

When user passes this:

/results?val=real&x=1&y=0

it should show:

/results/real.html?x=1&y=0

and from Action I should still be able to access $this->request->query['val'] which should be equal to real

What I have done so far?

I am using CakePHP 2.4

Router::parseExtensions('html');

Router::connect('/results/:val', 
            array('controller'=>'Post','action'=>'results',
'?' => array('val'=>'[A-Za-z0-9]-_ +','x'=>'[0-9]+','y'=>'[0-9]+')));

Solution

  • Just define the route as like below in your routes.php file.

    Router::connect(
        '/results/:val', 
        array(
            'controller' => 'Post',
            'action' => 'results',
        ), 
        array(
            'pass' => array('val')
        )
    );
    

    You can set the params like the below in order to generate the link in the way you want.

    echo Router::url(array(
        'controller' => 'Post',
        'action' => 'results',
        'val' => 'real',
        'ext' => 'html',
        '?'  => array('x' => '1', 'y' => '0')
    ));
    

    Which displays: results/real.html?x=1&y=0