tags:

views:

44

answers:

1

I plan to add up a calender function to my system, but i had mess up with the link which not link to the direct address that i want.. i am still new to cakephp, need some help to get understand the code and the correct way for me to point to the correct address i want..

here is some of the code in the controller, other side is not a problem to run just left the pointing address which had make me mad.. when i press to the link, which i want to view my data, it's just jump to the other side that not in my set up.

// here is the declaration which in the original controller that i learn from the net.

$view_base_url = $this->webroot. 'calendars';

//this the original link that i using which cant point to my correct side.

$data[$day] .= 'a href="'.$view_base_url.'/view/'. $v['Calendar']['id'] . '">' . $v['Calendar']['name'] . '</a>';

in the details, i wanted to link to my side with just the calendars/view/id of the link i point. but with this code, i will redirect to the app/webroot/view/id side. can i change the $this->webroot to just link to where i want?? if i not using the $this->webroot, i am not able to redirect while i click other page in the calendar..

it might be a poor explanation, cause i am still new to cakephp. just any 1 can kindly drop me a comment what i can do?

A: 

You should use the Router class to create links to within your application. For example:

Router::url(array('controller' => 'my_controller', 'action' => 'view', $id));
// returns, for example, /my_controller/view/5

Router::url(array('controller' => 'my_controller', 'action' => 'view', $id), true);
// returns, for example, http://example.com/root_directory/my_controller/view/5

This method is used by many functions throughout Cake, for example by the HTML Helper:

echo $html->link('My Link', array('controller' => 'my_controller', 'action' => 'view', $id));
// echos:
<a href="/my_controller/view/5">My Link</a>

The URLs that Router returns are based on the routes defined in app/config/routes.php. This way you can define handy shortcuts:

Router::connect('/view/*', array('controller' => 'my_controller', 'action' => 'view'));

echo $html->link('My Link', array('controller' => 'my_controller', 'action' => 'view', $id));
// echos:
<a href="/view/5">My Link</a>

This is the way you should handle all links in your Cake application. You shouldn't make links in the Controller, only in the View, using the HTML Helper. In the rare case were you do need links in the Controller, use Router::url().

deceze
ok thx for your information.. really helps a lot.
vincent low