9

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]+')));
Keval Domadia
  • 4,768
  • 1
  • 37
  • 64

1 Answers1

12

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

scrowler
  • 24,273
  • 9
  • 60
  • 92
Anil kumar
  • 4,107
  • 1
  • 21
  • 36
  • let me know if you have any query or concern keval sir @KarmicDice – Anil kumar Sep 30 '13 at 13:41
  • Thanks @Anil however, I am posting a GET form... so do you advice that I take the parameters and then echo Router::url because URL will be formed automatically... – Keval Domadia Oct 01 '13 at 10:44
  • how can i make it complete, just advise me, so i can make it as completed – Anil kumar Oct 01 '13 at 11:02
  • wait am playing around... hang in there... we have 6 days till it expires... so chill the f out... – Keval Domadia Oct 01 '13 at 11:34
  • ehm ... a very very very simple question: have you enabled the rewrite rules? – Daniele Salvatore Albano Oct 06 '13 at 22:00
  • I did sir. @daniele_dll didn't work... now I am taking in the request queries and then redirecting user using $this->redirect() with URL formed as mentioned in the second block of code by the answer-er – Keval Domadia Oct 07 '13 at 08:32
  • Just wanted to share in this question the whole explanation of all the options we have when dealing with routing params. Found at @dereuromark blog http://www.dereuromark.de/2013/05/04/passed-named-or-query-string-params/ – Djonatan Jul 17 '14 at 03:51