CakePHP 2: Html Helper in Email Template
I'm having a tough time trying to figure out why I can't use $this->Html->url in the templates for my emails for CakePHP 2.0. The Helpers array is empty in the views. Is this by design or am I missing something?
The docs and migration guide don't mention anything around this.
The trace looks like:
include - APP/View/Emails/html/admin/users_recover.ctp, line 1
View::_render() - CORE/Cake/View/View.php, l开发者_如何学Pythonine 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
Here is the function that I have sending the recovery email:
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;
}
And finally, the 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>
Have you tried setting CakeEmail renderer helpers before calling send()?
$cakeEmail->helpers('Html')
Bit more info at http://api20.cakephp.org/file/Cake/Network/Email/CakeEmail.php#method-CakeEmailhelpers
Another (anti-DRY) option could be to load HtmlHelper inside your templates:
<?php
$htmlHelper = $this->Helpers->load('Html');
$url = $htmlHelper->url(array('action' => 'recover', 'controller' => 'users', $recoveryKey), true);
?>
A) Make sure to include the relevant helpers in your controller (Html, in this case)
B) Use this book reference to check / set the Html helpers manually working.
精彩评论