将 CakePHP 从 2.6.2 更新到 2.7.2 后,在创建 auth flash 消息时出现丢失密钥错误。如何定义默认的元素模板authError
?
Since SessionComponent::setFlash()
已经已弃用 http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#displaying-notifications-or-flash-messages我添加了Flash组件 http://book.cakephp.org/2.0/en/core-libraries/components/flash.html in app/Controller/AppController.php
并修改了所有 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');
to this:
// 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
to
App/View/Elements/Flash/succeed.ctp
这是有效的——but如果我是未登录并尝试访问管理页面,我得到默认值授权错误 http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#AuthComponent::%24authError消息定义在app/Controller/AppController.php
shown without相应的模板。使用调试模式 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">
要获得使用我自己的元素模板“auth”呈现的默认 authError,需要对 AppController.php 进行哪些更改?
这是 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'),
)
);
当将所有控制器中的所有 Flash 消息更改为 Flash 组件和帮助程序时,这两行代码还需要吗? CakePHP 还用在什么地方?
echo $this->Session->flash();
echo $this->Session->flash('auth');
我也看了认证教程 http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html。但它似乎不是最新的$this->Session->setFlash()
仍在大量使用...