Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]+')));
like image 644
Karma Avatar asked Sep 19 '13 16:09

Karma


1 Answers

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

like image 171
Anil kumar Avatar answered Oct 18 '22 18:10

Anil kumar