本文介绍了如何定义FlashHelper / Component元素的一般authError消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将CakePHP从2.6.2更新到2.7.2之后,我在创建auth Flash消息时收到一个缺少的键错误。如何定义默认 authError


的元素模板

由于 SessionComponent :: setFlash()已被我添加了,并修改了此处的所有Flash邮件:

  // Controller 
$ this-> Session-> setFlash('Done','succeed');
$ this-> Session-> setFlash('有一个错误','failure');
$ this-> Session-> setFlash('请登录','auth');
//查看(默认布局)
echo $ this-> Session-> flash();
echo $ this-> Session-> flash('auth');

  // Controller 
$ this-> Flash-> succeed('Done');
$ this-> Flash-> failure('There is a error');
$ this-> Flash-> auth('请登录');
// View(default Layout)
echo $ this-> Flash-> render();
echo $ this-> Session-> flash(); //暂时保存?
echo $ this-> Session-> flash('auth'); //暂时保存?

我也从
复制了相关的模板$ /Elements/succeed.ctp
App / View / Elements / Flash / succeed.ctp



这是正常工作 - ,如果我未登录,并尝试访问管理页面我获得默认 message app / Controller / AppController.php 显示没有相应的模板。对于调试模式2,我得到以下错误:

  //未定义的变量: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,第418行
// SessionHelper :: flash() - CORE\Cake\View\Helper\SessionHelper.php,第159行
// include - APP\View\Layouts\default.ctp,第142行
// View :: _ evaluate() - CORE\Cake\View\View.php,第971行
//查看:: _ 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,第200行
// Dispatcher :: dispatch() - CORE\Cake \Routing\Dispatcher.php,第167行
// [main] - APP\webroot\index.php,第118行
//消息class =message>

需要使用AppController.php进行哪些更改才能使用自己的元素模板auth ?



这里是AppController.php的一部分:

  public $ components = array(
'Flash',
'Session',
'Security',
'Auth'=> array b $ b'authenticate'=> array('Form'=> array('passwordHasher'=>'Blowfish')),
'authError'=>'我的默认验证错误消息' //如何修改此行?
'loginAction'=> array('controller'=>'users','action'=>'login'),
'loginRedirect '=> array('controller'=>'users','action'=>'welcome'),
'logoutRedirect'=& action'=>'goodbye'),

);

将所有控制器中的所有闪存消息更改为Flash组件和帮助程序时, ? CakePHP在哪里使用它们?

  echo $ this-> Session-> flash (); 
echo $ this-> Session-> flash('auth');

我也看过。但它似乎不是最新的,因为 $ this-> Session-> setFlash()仍然在使用中...

$ b $

   >'Auth'= [
...
'flash'=> ['element'=> 'auth_error'],
...
]

Element / Flash 目录中命名为 auth_error.ctp 在这个文件中,你使用的唯一变量应该是 $ message ,因为当蛋糕调用Flash从Auth组件不通过任何其他变量(即 $ key 变量)



也许这个答案不是100%正确(所以欢迎任何建议)。 >

After updating CakePHP from 2.6.2 to 2.7.2 I get an missing key error when the auth flash message is created. How can I define the element template for the default authError?

Since SessionComponent::setFlash() has been deprecated I added the FlashComponent in app/Controller/AppController.php and modified all Flash messages from this:

// 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?

I also copied the flash related templates fromApp/View/Elements/succeed.ctp toApp/View/Elements/Flash/succeed.ctp

This is working – but if I am not logged in and try to access an admin page I get the default authError message defined in app/Controller/AppController.php shown without the according template. With debug mode 2 I get the following error:

// 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">

What changes in AppController.php are necessary to get the default authError rendered with my own element template "auth"?

Here the part of the 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'),
  )
);

And are these two lines still necessary when changing all flash messages in all Controllers to the Flash compoment and helper? Where else are they used by CakePHP?

echo $this->Session->flash();
echo $this->Session->flash('auth');

I also had a look at the Authentication tutorial. But it seems to be not up to date since $this->Session->setFlash() is still heavily in use...

解决方案

in your Auth component setting array add something like

'Auth' = [
    ...
    'flash' => ['element' => 'auth_error'],
    ...
]

then create a template named auth_error.ctp in your Element/Flash directory. In this file the only variable you use should be $message, because when cake calls Flash from the Auth components does not pass any other variable (i.e. the $key variable)

Maybe this answer is not 100% correct (so any suggestion is welcome) but it worked for me.

这篇关于如何定义FlashHelper / Component元素的一般authError消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 13:50