【问题标题】:CakePHP 2: Html Helper in Email TemplateCakePHP 2:电子邮件模板中的 Html 助手
【发布时间】:2011-09-23 23:59:08
【问题描述】:

我很难弄清楚为什么我不能在我的 CakePHP 2.0 电子邮件模板中使用 $this->Html->url。 Helpers 数组在视图中为空。这是设计使然还是我遗漏了什么?

文档和迁移指南没有提及这方面的任何内容。

跟踪看起来像:

include - APP/View/Emails/html/admin/users_recover.ctp, line 1
View::_render() - CORE/Cake/View/View.php, line 598
View::render() - CORE/Cake/View/View.php, line 365
CakeEmail::_render() - CORE/Cake/Network/Email/CakeEmail.php, line 1300
CakeEmail::send() - CORE/Cake/Network/Email/CakeEmail.php, line 933
UsersController::_sendRecoverEmail() - APP/Controller/UsersController.php, line 186
UsersController::admin_login() - APP/Controller/UsersController.php, line 101
ReflectionMethod::invokeArgs() - [internal], line ??
Controller::invokeAction() - CORE/Cake/Controller/Controller.php, line 476
Dispatcher::_invoke() - CORE/Cake/Routing/Dispatcher.php, line 106
Dispatcher::dispatch() - CORE/Cake/Routing/Dispatcher.php, line 88
[main] - APP/webroot/index.php, line 96

这是我发送恢复电子邮件的功能:

private function _sendRecoverEmail($email, $recoverKey) {
    App::uses('CakeEmail', 'Network/Email');
    $cakeEmail = new CakeEmail();
    $cakeEmail->config('default');
    $cakeEmail->to($email);
    $cakeEmail->subject('Recover Your '. Configure::read('Site.title') . ' Account');
    $cakeEmail->template('admin/users_recover');
    $cakeEmail->viewVars(array(
        'recoverKey' => $recoverKey,
    ));
    try {
        $cakeEmail->send();
    } catch (Exception $e) {
        trigger_error("ERROR in UsersController::sendRecoverEmail: couldn't send email to: {$email}.");
        return false;
    }
    return true;
}   

最后是视图:

<p>Have you been having difficulty getting into your <?php echo Configure::read('Site.title'); ?> account?</p>

<?php $url = $this->Html->url(array('action' => 'recover', 'controller' => 'users', $recoveryKey), true); ?> 

<p>If this is correct, <a href="<?php echo $url; ?>">click here to recover your access and create a new password.</a></p>

<p>Your password will remain the same if you ignore it.</p>

【问题讨论】:

  • 您能否粘贴一些示例代码和相关文件路径以显示您的问题所在?

标签: cakephp


【解决方案1】:

您是否尝试在调用 send() 之前设置 CakeEmail 渲染器助手?

$cakeEmail->helpers('Html')

更多信息请访问http://api20.cakephp.org/file/Cake/Network/Email/CakeEmail.php#method-CakeEmailhelpers

另一个(反 DRY)选项可能是在模板中加载 HtmlHelper:

<?php
$htmlHelper = $this->Helpers->load('Html');

$url = $htmlHelper->url(array('action' => 'recover', 'controller' => 'users', $recoveryKey), true);
?>

【讨论】:

    【解决方案2】:

    A) 确保在您的控制器中包含相关帮助程序(在本例中为 Html)

    B) 使用this book reference 手动检查/设置 Html 帮助程序。

    【讨论】:

    • @timing 来自堆栈跟踪,很明显他使用的是UsersController
    猜你喜欢
    • 1970-01-01
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-05
    • 2010-12-16
    • 2014-07-05
    相关资源
    最近更新 更多