I'm trying to retrieve a list of all the routes contained in app/Config/routes.php and show them on an admin page.
I'm able to retrieve a list of controllers using $controllers = App::objects('controller');
and I was wondering if it's possible to do the same for routes.
I've tried using substrings as per the code below but problems that come to mind are commented out routes, white spaces and variations in routes, e.g. links to external resources. I'm now considering using php's tokenizer but I'd like to know if there is a simple and elegant solution built into CakePHP.
$source = file_get_contents(APP . 'Config/routes.php');
$startPos = stripos($source, 'Router::connect(');
$routes = array();
while ($startPos !== false) {
$endPos = stripos($source, ';', $startPos + 15);
if($endPos !== false) {
$route = substr($source, $startPos, $endPos - $startPos);
$urlStart = stripos($route, "'");
if($urlStart !== false) {
$urlEnd = stripos($route, "'", $urlStart + 1);
$url = substr($route, $urlStart + 1, $urlEnd - $urlStart - 1);
$routes[] = array('route'=>$route, 'url'=>$url);
}
$startPos = stripos($source, 'Router::connect(', $endPos + 1);
}
}