I am trying to implement routing in cakephp. I want the urls to mapped like this...
www.example.com/nodes/main -> www.example.com/main www.example.com/nodes/about -> www.example.com/about
So for this I wrote in my config/routes.php file..
Router::connect('/:action', a开发者_StackOverflowrray('controller' => 'nodes'));
Now, I got the thing going but when I click on the links, the url in browser appears like www.example.com/nodes/main www.example.com/nodes/about
Is there some way where I can get the urls to appear the way they are routed? Setting in .htaccess or httpd.conf would be easy - but I don't have access to that.
Regards Vikram
This should work:
Router::connect('/main', array('controller' => 'nodes', 'action' => 'main'));
Router::connect('/about', array('controller' => 'nodes', 'action' => 'about'));
You may also do something more powerful, like this:
$actions = array('main','about');
foreach ($actions as $action){
Router::connect('/$action', array('controller' => 'nodes', 'action' => '$action'));
}
Basically if your links are created with Html helper, with the following format:
<?php echo $this->Html->link('your link', array('controller'=>'nodes', 'action'=>'main'));?>
Then the Cake will convert the links properly to www.example.com/main
But if your links are
<?php echo $this->Html->link('your link', '/nodes/main/');?>
they will point to www.example.com/nodes/main
精彩评论