如何为一般 authError 消息定义 FlashHelper/Component 元素
How to define FlashHelper/Component element for general authError message
将 CakePHP 从 2.6.2 更新到 2.7.2 后,我在创建 auth flash 消息时遇到了丢失密钥错误。如何定义默认 authError
的元素模板?
由于 SessionComponent::setFlash()
已在 app/Controller/AppController.php
中 deprecated I added the FlashComponent 并修改了所有 Flash 消息:
// Controller
$this->Session->setFlash('Done', 'succeed');
$this->Session->setFlash('There is an error', 'failure');
$this->Session->setFlash('Please log in', 'auth');
// View (default Layout)
echo $this->Session->flash();
echo $this->Session->flash('auth');
对此:
// Controller
$this->Flash->succeed('Done');
$this->Flash->failure('There is an error');
$this->Flash->auth('Please log in');
// View (default Layout)
echo $this->Flash->render();
echo $this->Session->flash(); // keep temporarily?
echo $this->Session->flash('auth'); // keep temporarily?
我也复制了flash相关的模板
App/View/Elements/succeed.ctp
到
App/View/Elements/Flash/succeed.ctp
这是有效的 – 但是如果我未登录并尝试访问管理页面我得到默认值authError app/Controller/AppController.php
中定义的消息显示 没有 相应的模板。使用调试模式 2,我收到以下错误:
// Undefined variable: key [CORE\Cake\View\Elements\Flash\default.ctp, line 1]
// include - CORE\Cake\View\Elements\Flash\default.ctp, line 1
// View::_evaluate() - CORE\Cake\View\View.php, line 971
// View::_render() - CORE\Cake\View\View.php, line 933
// View::_renderElement() - CORE\Cake\View\View.php, line 1227
// View::element() - CORE\Cake\View\View.php, line 418
// SessionHelper::flash() - CORE\Cake\View\Helper\SessionHelper.php, line 159
// include - APP\View\Layouts\default.ctp, line 142
// View::_evaluate() - CORE\Cake\View\View.php, line 971
// View::_render() - CORE\Cake\View\View.php, line 933
// View::renderLayout() - CORE\Cake\View\View.php, line 546
// View::render() - CORE\Cake\View\View.php, line 481
// Controller::render() - CORE\Cake\Controller\Controller.php, line 960
// Dispatcher::_invoke() - CORE\Cake\Routing\Dispatcher.php, line 200
// Dispatcher::dispatch() - CORE\Cake\Routing\Dispatcher.php, line 167
// [main] - APP\webroot\index.php, line 118
// Message" class="message">
在 AppController.php 中需要做哪些更改才能使用我自己的元素模板 "auth" 呈现默认的 authError?
这里是AppController.php的部分:
public $components = array(
'Flash',
'Session',
'Security',
'Auth' => array(
'authenticate' => array('Form' => array('passwordHasher' => 'Blowfish')),
'authError' => 'My default auth error message.', // How do I have to modify this line?
'loginAction' => array('controller' => 'users', 'action' => 'login'),
'loginRedirect' => array('controller' => 'users', 'action' => 'welcome'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'goodbye'),
)
);
并且将所有Controller中的所有flash消息更改为Flash组件和助手时,是否还需要这两行? CakePHP 在其他什么地方使用它们?
echo $this->Session->flash();
echo $this->Session->flash('auth');
我也看过 Authentication tutorial。但它似乎不是最新的,因为 $this->Session->setFlash()
仍在大量使用...
在您的 Auth 组件设置数组中添加类似
的内容
'Auth' = [
...
'flash' => ['element' => 'auth_error'],
...
]
然后在您的 Element/Flash
目录中创建一个名为 auth_error.ctp
的模板。在此文件中,您使用的唯一变量应该是 $message
,因为当 cake 从 Auth 组件调用 Flash 时不会传递任何其他变量(即 $key
变量)
也许这个答案不是 100% 正确(所以欢迎任何建议)但它对我有用。
这是 Cake 本身的一个错误,它已(将)在 2.7.4 中修复
只需添加 Flash 组件即可。
class AppController extends Controller {
public $components = array('DebugKit.Toolbar','Flash');
}
我也遇到了同样的问题,这一定能解决你的问题。请在app/Controller/AppController.php中添加如下代码行:
public $components = array('Flash');
将 CakePHP 从 2.6.2 更新到 2.7.2 后,我在创建 auth flash 消息时遇到了丢失密钥错误。如何定义默认 authError
的元素模板?
由于 SessionComponent::setFlash()
已在 app/Controller/AppController.php
中 deprecated I added the FlashComponent 并修改了所有 Flash 消息:
// Controller
$this->Session->setFlash('Done', 'succeed');
$this->Session->setFlash('There is an error', 'failure');
$this->Session->setFlash('Please log in', 'auth');
// View (default Layout)
echo $this->Session->flash();
echo $this->Session->flash('auth');
对此:
// Controller
$this->Flash->succeed('Done');
$this->Flash->failure('There is an error');
$this->Flash->auth('Please log in');
// View (default Layout)
echo $this->Flash->render();
echo $this->Session->flash(); // keep temporarily?
echo $this->Session->flash('auth'); // keep temporarily?
我也复制了flash相关的模板
App/View/Elements/succeed.ctp
到
App/View/Elements/Flash/succeed.ctp
这是有效的 – 但是如果我未登录并尝试访问管理页面我得到默认值authError app/Controller/AppController.php
中定义的消息显示 没有 相应的模板。使用调试模式 2,我收到以下错误:
// Undefined variable: key [CORE\Cake\View\Elements\Flash\default.ctp, line 1]
// include - CORE\Cake\View\Elements\Flash\default.ctp, line 1
// View::_evaluate() - CORE\Cake\View\View.php, line 971
// View::_render() - CORE\Cake\View\View.php, line 933
// View::_renderElement() - CORE\Cake\View\View.php, line 1227
// View::element() - CORE\Cake\View\View.php, line 418
// SessionHelper::flash() - CORE\Cake\View\Helper\SessionHelper.php, line 159
// include - APP\View\Layouts\default.ctp, line 142
// View::_evaluate() - CORE\Cake\View\View.php, line 971
// View::_render() - CORE\Cake\View\View.php, line 933
// View::renderLayout() - CORE\Cake\View\View.php, line 546
// View::render() - CORE\Cake\View\View.php, line 481
// Controller::render() - CORE\Cake\Controller\Controller.php, line 960
// Dispatcher::_invoke() - CORE\Cake\Routing\Dispatcher.php, line 200
// Dispatcher::dispatch() - CORE\Cake\Routing\Dispatcher.php, line 167
// [main] - APP\webroot\index.php, line 118
// Message" class="message">
在 AppController.php 中需要做哪些更改才能使用我自己的元素模板 "auth" 呈现默认的 authError?
这里是AppController.php的部分:
public $components = array(
'Flash',
'Session',
'Security',
'Auth' => array(
'authenticate' => array('Form' => array('passwordHasher' => 'Blowfish')),
'authError' => 'My default auth error message.', // How do I have to modify this line?
'loginAction' => array('controller' => 'users', 'action' => 'login'),
'loginRedirect' => array('controller' => 'users', 'action' => 'welcome'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'goodbye'),
)
);
并且将所有Controller中的所有flash消息更改为Flash组件和助手时,是否还需要这两行? CakePHP 在其他什么地方使用它们?
echo $this->Session->flash();
echo $this->Session->flash('auth');
我也看过 Authentication tutorial。但它似乎不是最新的,因为 $this->Session->setFlash()
仍在大量使用...
在您的 Auth 组件设置数组中添加类似
的内容'Auth' = [
...
'flash' => ['element' => 'auth_error'],
...
]
然后在您的 Element/Flash
目录中创建一个名为 auth_error.ctp
的模板。在此文件中,您使用的唯一变量应该是 $message
,因为当 cake 从 Auth 组件调用 Flash 时不会传递任何其他变量(即 $key
变量)
也许这个答案不是 100% 正确(所以欢迎任何建议)但它对我有用。
这是 Cake 本身的一个错误,它已(将)在 2.7.4 中修复
只需添加 Flash 组件即可。
class AppController extends Controller {
public $components = array('DebugKit.Toolbar','Flash');
}
我也遇到了同样的问题,这一定能解决你的问题。请在app/Controller/AppController.php中添加如下代码行:
public $components = array('Flash');