更新CakePHP从2.6.2到2.7.2后,当创建验证flash消息时,我得到一个缺失的密钥错误。如何为默认的authError
定义元素模板?
由于SessionComponent::setFlash()
已被弃用,我在app/Controller/AppController.php
中添加了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
这是工作- 但如果我未登录并尝试访问管理页面,我得到app/Controller/AppController.php
中定义的默认作者错误消息,显示而没有根据模板。在调试模式2下,我得到以下错误:
// Undefined variable: key [CORECakeViewElementsFlashdefault.ctp, line 1]
// include - CORECakeViewElementsFlashdefault.ctp, line 1
// View::_evaluate() - CORECakeViewView.php, line 971
// View::_render() - CORECakeViewView.php, line 933
// View::_renderElement() - CORECakeViewView.php, line 1227
// View::element() - CORECakeViewView.php, line 418
// SessionHelper::flash() - CORECakeViewHelperSessionHelper.php, line 159
// include - APPViewLayoutsdefault.ctp, line 142
// View::_evaluate() - CORECakeViewView.php, line 971
// View::_render() - CORECakeViewView.php, line 933
// View::renderLayout() - CORECakeViewView.php, line 546
// View::render() - CORECakeViewView.php, line 481
// Controller::render() - CORECakeControllerController.php, line 960
// Dispatcher::_invoke() - CORECakeRoutingDispatcher.php, line 200
// Dispatcher::dispatch() - CORECakeRoutingDispatcher.php, line 167
// [main] - APPwebrootindex.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'),
)
);
当所有控制器中的所有flash消息更改为flash组件和helper时,这两行仍然是必要的吗?CakePHP还在哪里使用它们?
echo $this->Session->flash();
echo $this->Session->flash('auth');
我还看了一下身份验证教程。但它似乎不是最新的,因为$this->Session->setFlash()
仍然大量使用…
在Auth组件设置数组中添加如下内容
'Auth' = [
...
'flash' => ['element' => 'auth_error'],
...
]
然后在Element/Flash
目录中创建一个名为auth_error.ctp
的模板。在这个文件中,你使用的唯一变量应该是$message
,因为当蛋糕从Auth组件调用Flash时,不会传递任何其他变量(即$key
变量)
也许这个答案不是100%正确(所以欢迎任何建议),但它对我有用
这是Cake本身的一个错误,它(将)在2.7.4
修复见:https://github.com/cakephp/cakephp/pull/7379
只要添加Flash组件就可以了。
class AppController extends Controller {
public $components = array('DebugKit.Toolbar','Flash');
}
我也遇到过同样的问题,这一定会解决你的问题。请在app/Controller/AppController.php中添加以下代码行:
public $components = array('Flash');