Jonathan Acosta
Jonathan Acosta

Reputation: 89

CakePHP routing with GET params

I'm currently porting a website from WordPress to CakePHP, and I have a problem with one of the routes used in the WP version.

I have to route http://example.com/?specific_key=value to http://example.com/controller/action but I can't find a way to catch the url with the params before the default home page route catches it.

Any advice or pointers are greatly appreciated.

Thanks!

Upvotes: 1

Views: 1793

Answers (2)

tigrang
tigrang

Reputation: 6767

Try adding the following to the top of your routes file. Check if the param is set with the value and connect '/' to where you need it to route to. This way it will match your route before pages controller route.

if (isset($_GET['specific_key']) && $_GET['specific_key'] === 'value') {
    Router::connect('/', array(whatever controller/action you need));
}

// the rest of your routes

You could also create a rewrite rule for it in your htaccess file:

RewriteCond %{QUERY_STRING} ^specific_key=value$ [NC]
RewriteRule / /controller/action? [R=301,L]

Third Option: Use a custom route class: https://gist.github.com/3763800

Create the file APP/Lib/Route/QueryStringRoute.php and put the contents of the above link in it.

Then in your routes do:

App::uses('QueryStringRoute', 'Route');
Router::connect('/', array('controller' => 'your_controller'), array(
    'routeClass' => 'QueryStringRoute', 
    'query' => array('specific_key' => 'value')
));

Again, it would need to be before your home route.

Edit: Added third option.

Upvotes: 4

ADmad
ADmad

Reputation: 8100

Setting up routes based on GET params is not possible.

Edit: Using the inbuilt CakeRoute class atleast.

Upvotes: 1

Related Questions