I am trying to test some new features on a live site.
My goal is to have a prefix (‘dev’, for example) and have that prefix display the complete site with default actions, except those I define as dev_{action_name}.
Example:
/dev/orders/create
Seeks for OrdersController::dev_create() action, if not found, uses OrdersController::create().
So far, I managed to make a standard ‘dev’ prefix with the Router and put some beforeFilter magic like:
<?php
//bootstrap.php code:
Configure::write('Routing.prefixes', array('admin', 'dev'));
// AppController::beforeFilter() code:
if (preg_match('/^dev_(.*)$/', $this->params['action'], $subs)){
$new_action = $subs[1];
if (method_exists($this, $new_action)){
$this->params['action'] = $new_action;
}
}
?>
This works as expected, except the complete Routing schema is not copied to “dev”, which means all the links are rendered without the “dev prefix and my custom routes are disregarded.
I thought about fixing it with .htaccess rule and putting a dev=true in the query string:
#webroot/.htaccess
RewriteRule ^dev(.*)$ index.php?dev=true&%{QUERY_STRING}
#... rewrite conds ...
RewriteRule ^(.*)$ index.php [QSA,L] #original Cake rewrite
But this yielded no results (Cake was still parsing the original URL, not the re-written).
Is there a “right” way of doing this?
I have successfully achieved this with CakePHP 2.2.1. It’s ugly, it’s long, it’s against the rules, but it works! 😀
file: /app/webroot/.htaccess
file: bootstrap.php
At this point, we have all that we need to have CakePHP display a complete copy of the site under /dev/.
Cake does all the default actions+views, all links work as expected (as long as you use helpers for URLs), all static files get served.
All app paths and constants remain intact.
Now for some method magic:
All of this enables us to:
For instance: