将 CakePHP 从 2.6.2 更新到 2.7.2 后,在创建 auth flash 消息时出现缺少密钥错误。如何定义默认的元素模板authError
?
由于SessionComponent::setFlash()
已被弃用,我添加了FlashComponent并从中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');
对此:
// 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'),
)
);
将所有控制器中的所有闪存消息更改为闪存组件和助手时,这两行是否仍然必要?CakePHP 还在哪里使用它们?
echo $this->Session->flash();
echo $this->Session->flash('auth');
我还查看了身份验证教程。但它似乎不是最新的,因为$this->Session->setFlash()
仍在大量使用......