Skip to content

Instantly share code, notes, and snippets.

@ThatGirlSlays
Created January 30, 2013 16:41
Show Gist options
  • Save ThatGirlSlays/9ba0a54a7f4d68803d14 to your computer and use it in GitHub Desktop.
Save ThatGirlSlays/9ba0a54a7f4d68803d14 to your computer and use it in GitHub Desktop.
CakePHP 2.2.4 Routes Snippet
// Break url into params
$requestURI = explode('/', $_SERVER['REQUEST_URI']);
$scriptName = explode('/',$_SERVER['SCRIPT_NAME']);
for($i= 0;$i < sizeof($scriptName);$i++)
{
if ($requestURI[$i] == $scriptName[$i])
{
unset($requestURI[$i]);
}
}
$param = array_values($requestURI);
// Only first 2 params are important
$param1 = $param[0];
$param2 = $param[1];
// Check to see if the first param matches a Page.link, it if does, do custom routing for page
if($param1 != "users" && $param1){
$pagelink = str_replace('-', ' ', $param1);
$pagesModel = ClassRegistry::init('Page');
$matchedpage = $pagesModel->find('first', array(
'conditions' => array('Page.link LIKE' => "$pagelink"), 'recursive' => '-1'
));
if($matchedpage['Page']['id']){
// domain.com/potc
Router::connect('/' . $param1, array('controller' => 'pages', 'action' => 'view', 'page_id' => $matchedpage['Page']['id']), array(
'pass' => array('page_id'),
'page_id' => '[0-9]+'
));
// domain.com/potc/articles
Router::connect('/' . $param1 . '/:controller', array('action' => 'index', 'page_id' => $matchedpage['Page']['id']), array(
'pass' => array('page_id'),
'page_id' => '[0-9]+'
));
// domain.com/potc/articles/edit/99
Router::connect('/' . $param1 . '/:controller/:action/:id', array('page_id' => $matchedpage['Page']['id']), array(
'pass' => array('page_id', 'id'),
'page_id' => '[0-9]+'
));
// domain.com/potc/articles/99
Router::connect('/' . $param1 . '/:controller/:id', array('action' => 'view', 'page_id' => $matchedpage['Page']['id']), array(
'pass' => array('page_id', 'id'),
'page_id' => '[0-9]+',
'id' => '[0-9]+'
));
// domain.com/potc/articles/topics
Router::connect('/' . $param1 . '/:controller/:action', array('page_id' => $matchedpage['Page']['id']), array(
'pass' => array('page_id'),
'page_id' => '[0-9]+'
));
// other custom routing if Page.link matches
// other custom routing if Page.link matches
// other custom routing if Page.link matches
} // end if page matches
} // end if param1 is not users
// other routing
// other routing
// other routing
// Below matches routing if page does not have a Page.link
// domain.com/movies/106
Router::connect(
'/:category/:page_id',
array('controller' => 'pages', 'action' => 'view'),
array(
'pass' => array('page_id'),
'page_id' => '[0-9]+',
'category' => 'shows|music|games|books|hobbies'
)
);
// MODULE INDEX
Router::connect(
'/:category/:page_id/:controller',
array('action' => 'index'),
array(
'pass' => array('page_id'),
'page_id' => '[0-9]+',
'category' => 'shows|music|games|books|hobbies'
)
);
// domain.com/movies/106/articles/edit/106
Router::connect(
'/:category/:page_id/:controller/:action/:id',
array(),
array(
'pass' => array('page_id', 'id', 'category'),
'page_id' => '[0-9]+',
'category' => 'shows|music|games|books|hobbies'
)
);
// domain.com/movies/106/articles/106
Router::connect(
'/:category/:page_id/:controller/:id',
array('action' => 'view'),
array(
'pass' => array('page_id', 'id', 'category'),
'page_id' => '[0-9]+',
'category' => 'shows|music|games|books|hobbies'
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment