Skip to content

Instantly share code, notes, and snippets.

@tigrang
Created September 21, 2012 20:47
Show Gist options
  • Save tigrang/3763800 to your computer and use it in GitHub Desktop.
Save tigrang/3763800 to your computer and use it in GitHub Desktop.
A terrible and terribly simple implementation of a route class that matches get params
<?php
App::uses('CakeRoute', 'Routing/Route');
/**
* QueryStringRoute
*
* Usage:
*
* App::uses('QueryRoute', 'Route');
* Router::connect('/', ['controller' => 'users'], ['routeClass' => 'QueryStringRoute', 'query' => ['foo' => 'bar']]);
*
* This will map the url site.com/?foo=bar to UsersController::index()
*/
class QueryStringRoute extends CakeRoute {
/**
* Constructor for a Route
*
* @param string $template Template string with parameter placeholders
* @param array $defaults Array of defaults for the route.
* @param array $options Array of additional options for the Route
*/
public function __construct($template, $defaults = array(), $options = array()) {
parent::__construct($template, $defaults, $options);
$this->options = array_merge(['query' => null], $options);
}
/**
* Parses route
*
* @param string $url
* @return bool|array
*/
public function parse($url) {
$route = parent::parse($url);
$requestQuery = Router::getRequest(true)->query;
$checkQuery = (array)$this->options['query'];
return ($route && $requestQuery === $checkQuery) ? $route : false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment