在这种情况下如何指向我的正确地址?!需要帮忙

时间:2009-11-06 04:04:56

标签: cakephp

我打算在我的系统中添加一个日历功能,但是我已经搞砸了没有链接到我想要的直接地址的链接。 我仍然是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-&gt; webroot更改为只链接到我想要的地方吗? 如果我没有使用$ this-&gt; webroot,当我点击日历中的其他页面时,我无法重定向..

这可能是一个糟糕的解释,因为我仍然是cakephp的新手。 只有任何一个人可以请我发表评论我能做什么?

1 个答案:

答案 0 :(得分: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

这个方法被整个Cake中的许多函数使用,例如HTML Helper:

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中创建链接。在极少数情况下,您需要在Controller中使用链接,请使用Router::url()