Routes Component
This is a component to add/remove routes directly to/from the /app/config/routes.php file.
You can use this if you are building a content management system or something else where you may not be around to manually create routes for your app.
Please keep in mind that for this to work your web server needs read/write permission to your /app/config/routes.php file. Short story short...here is the code:
Please keep in mind that for this to work your web server needs read/write permission to your /app/config/routes.php file. Short story short...here is the code:
Component Class:
<?php
class RouteComponent extends Object {
var $route_file = '../config/routes.php';
function initialize() {
if (!is_file($this->route_file)) {
die('The path to your route file is wrong. Edit /app/controllers/components/route.php and fix the problem.');
}
}
function add($route) {
$route = $route."\n";
if (is_writable($this->route_file)) {
$routes = file($this->route_file);
$new_routes = '';
foreach ($routes as $i) {
if (trim($i) != '?>') {
$new_routes .= $i;
} else break;
}
$handle = fopen($this->route_file, 'w');
if (fwrite($handle, $new_routes.$route.'?>')) {
return true;
} else return false;
fclose($handle);
}else return false;
}
function remove($route) {
$route = $route."\n";
if (is_writable($this->route_file)) {
$routes = file($this->route_file);
$new_routes = '';
foreach ($routes as $i) {
if (trim($i) != '?>') {
if ($i != $route) {
$new_routes .= $i;
}
} else break;
}
$handle = fopen($this->route_file, 'w');
if (fwrite($handle, $new_routes.'?>')) {
return true;
} else return false;
fclose($handle);
} else return false;
}
/* Suggested by José Pedro Saraiva */
function check( $route ) {
$route = $route . "\n";
if (is_writable( $this->route_file )) {
$routes = file( $this->route_file );
$new_routes = '';
foreach ( $routes as $i ) {
if (trim( $i ) != '?>') {
if ($i == $route) {
return true;
}
} else
break;
}
}
return false;
}
}
?>
Usage:
Controller Class:
<?php
// To create a new route
$route->add("Router::connect('/test', array('controller' => 'pages', 'action' => 'display', 'test'));");
// To remove a route
$route->remove("Router::connect('/test', array('controller' => 'pages', 'action' => 'display', 'test'));");
?>








William Wallace is a spammer, he had posted the similar post on several other articles. This is the reason we have decided to unpublish his comments on the bakery (which were all the same) along with the replies on the comments.
So please don't republish his comments.
Thank you!
Here is an example for default Pages Controller
first you have to create a model where you will be storing page content and name, title and whatever..
then in routes.php file add something like this code, which lets automatically add routes to your site:
<?php
You can even create a pages model as actsAs(tree), then you can load tree based menu..//Load all static pages
App::import('Model', 'Page');
$page_model = new Page();
//where is a page slug in each record to identify route
$pages = $page_model->find('all', array('fields' => array(EHS_PAGE_SLUG)));
unset($page_model);
foreach($pages as $page) {
Router::connect(
"/{$page['Page']['slug']}",
array(
'controller' => 'pages',
'action' => 'display',
$page['Page']['slug']
)
);
}
?>
Just a small contribution, added a 'check' function to check if a given rule exists
function check( $route )
{
$route = $route . "\n";
if (is_writable( $this->route_file )) {
$routes = file( $this->route_file );
$new_routes = '';
foreach ( $routes as $i ) {
if (trim( $i ) != '?>') {
if ($i == $route) {
return true;
}
} else
break;
}
}
return false;
}
Thanks José!
I added your function. I hope other people can also find this helpful.
Could you please clarify the purpose of this approach?
And why would one chose adding this, vs simply editing the file?
There are several uses for the ability to create routes on the fly. For me personally I was building a content management system and I can't just jump in and edit my routes file when somebody creates a page.
Comments are closed for articles over a year old