views:

61

answers:

2

Hello all

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', array('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

+2  A: 

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'));
}
santiagobasulto
Hi thereThis is already working for me using the code I wrote. Its just the issue of URL appearing in address bar that I am looking to address.
ShiVik
uhm, i've seen that happeining. I don't remember why, right now but i'll take a look. Have you try with the writing the controllers? Becouse in your code those are missing, then, are interpreted like the actual controller working.
santiagobasulto
All right! That was the problem. I wasn't writing the controller when using the link helper, and since I had default controller set "nodes", that is why it was displaying the controller as well as action in the address bar. Thanks man.
ShiVik
You're welcome brother.
santiagobasulto
+2  A: 

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

Nik