2009-11-06 67 views
0

我打算爲我的系統添加一個日曆功能,但是我已經搞亂了那些沒有鏈接到我想要的直接地址的鏈接.. 我對cakephp仍然很陌生,需要一些幫助才能理解代碼和正確的方法讓我指向我想要的正確地址。如何在這種情況下指出我的正確地址?需要幫助

這裏是控制器中的一些代碼,其他方面不是運行問題,只是留下了讓我發瘋的指向地址 當我按下鏈接,我想查看我的數據,它只是跳轉到另一端,而不是在我的設置。

// 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>'; 
在細節

, 我想只用日曆/視圖/我點了鏈接的ID鏈接到我的身邊。 但使用此代碼,我將重定向到app/webroot/view/id端。 我可以更改$ this-> webroot以鏈接到我想要的位置? 如果我不使用$這個 - >根目錄,我不能夠重定向,而我點擊日曆中的其他網頁..

它可能是一個貧窮的解釋,因爲我是還是新把CakePHP。 只要任何一個可以請我評論我能做什麼?

回答

1

您應該使用Router類來創建應用程序內的鏈接。例如:

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 

該方法用於由許多功能在整個蛋糕,例如由HTML輔助:

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

Router返回基於在app/config/routes.php定義的路由的網址。這樣你可以定義方便的快捷鍵:

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> 

這是你應該處理Cake應用程序中所有鏈接的方式。您不應在控制器中創建鏈接,只能在視圖中使用HTML助手。在極少數情況下,您確實需要Controller中的鏈接,請使用Router::url()

+0

確定thx爲您的信息..真的有很大的幫助。 – 2009-11-10 06:18:08

相關問題