CakePHP 2 : Html Helper in Email Template

标签 cakephp

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

文档和迁移指南对此没有提及任何内容。

跟踪看起来像:

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;
}   

最后, View :

<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>

最佳答案

您是否尝试过在调用 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);
?>

关于CakePHP 2 : Html Helper in Email Template,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7535979/

相关文章:

php - Gmail自动登录脚本

javascript - 如何使用 cakePHP 执行 ajax 请求?

mysql - 在 cakephp 中从一个模型连接两个模型

session - cakephp 多个应用程序具有相同的蛋糕库和共享 session

php - 如何在cakephp中以逗号值分隔搜索

php - MySQL 中带有加法运算符的 IF/ELSE 语句

php - Cakephp模型忽略belongsTo中的条件

php - Cakephp 3.x 记住我登录功能

mysql - 我可以在 CakePHP 中重新创建以下 MySQL 查询吗?

validation - Cakephp3 将(自定义)验证传递给 flash 消息