我刚刚开始使用cakePHP,到目前为止,事情进展得并不顺利。
我有一个控制器来处理确认用户的电子邮件。在注册时,会向用户发送一封电子邮件,其中包含链接中的确认码。根据它们给出的确认码,控制器会给出不同的文本响应。其中一个响应包括用于登录的超链接。
我正在尝试使用helper,但是虽然我已经将它加载到类的顶部的$helpers中,但只有使用App::import,然后实例化它,我才能让它工作。
简单地做一个超链接看起来有点夸张!我需要加载同一个类多少次?
无论我在网页上看什么,它总是告诉我在控制器中使用助手不是一个好主意,但是我还能怎么做这个链接呢?
所以我有
var $helpers = array('Html');在控制器的顶部,并且:
if (isset($this->User->id)) { // Check the user's entered it right
// Do some stuff to remember the user has confirmed
// This is to load the html helper - supposedly bad form, but how else do I make the link?
App::import('Helper', 'Html');
$html = new HtmlHelper();
$this->set('message', __("Your email address has been confirmed.", TRUE)." ".$html->link(__("Please log in", TRUE), array('controller' => "users", 'action' => "login" )));
} else {
$this->set('message', __("Please check your mail for the correct URL to confirm your account", TRUE));
}在控制器的confirm方法和
<div>
<?php echo $message;?>
</div>在视图中输出结果消息
我肯定是在哪里出错了--有人能解释一下是怎么回事吗?
发布于 2010-09-28 09:08:17
其思想是使用set将呈现页面所需的所有数据发送到视图,然后使用helper在视图中完成任何条件逻辑或格式设置,因此在适当的时候发送整个查询结果(假设您需要更改一个链接以包含用户的屏幕名称,您将非常方便)。
在控制器操作中
$this->set('user', $this->User);在视图中(根据是在<= 1.2中还是在1.3中,这会略有不同
if ($user->id) //available because of Controller->set
{
//1.2
$link = $html->link(__("Please log in", TRUE), array('controller' => "users", 'action' => "login" ));
//1.3
$link = $this->Html->link(__("Please log in", TRUE), array('controller' => "users", 'action' => "login" ));
echo __("Your email address has been confirmed.", TRUE)." $link";
}
else
{
$this->set('message', __("Please check your mail for the correct URL to confirm your account", TRUE));
}发布于 2010-09-28 09:31:30
你不应该在控制器中使用辅助对象。正如@Lincoln指出的那样,您应该在视图中构建链接。您可以在控制器中构造HTML,因为基本上就是数据,而链接是一个非常特定于媒介的()实现。
无论哪种方式,如果您想通过电子邮件发送它,您都需要创建一个完整的URL (包括主机)。最通用的方法是使用Router::url
$fullUrl = Router::url(array('controller' => ...), true); // 'true' for full URL在控制器或视图中执行此操作。要创建链接,请在视图中使用以下内容:
echo $html->link('Title', $fullUrl);发布于 2010-09-29 03:22:53
您想要做的事情应该通过SessionComponent来完成。$this->Session->setFlash(‘你的消息在这里’);
在您的布局中,使用session helper将$this->Session->flash();
关于您想要在控制器中使用的url,Router::url是正确的,正如deceze所说,但是它没有任何用处,因为您不应该在控制器中构建html。
您要做的是使用上面的session::setFlash()方法,然后使用
$this->redirect(array('controller‘登录“用户”,’=>‘=>“登录”));
https://stackoverflow.com/questions/3808883
复制相似问题