3

将 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.ctpApp/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()仍在大量使用......

4

4 回答 4

2

在您的 Auth 组件设置数组中添加类似

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

auth_error.ctp然后在您的Element/Flash目录中创建一个名为的模板。在这个文件中,您使用的唯一变量应该是$message,因为当 cake 从 Auth 组件调用 Flash 时不会传递任何其他变量(即$key变量)

也许这个答案不是 100% 正确的(所以欢迎任何建议),但它对我有用。

于 2015-08-17T08:41:37.810 回答
0

这是 Cake 本身的一个错误,它(将)在 2.7.4 中修复

见:https ://github.com/cakephp/cakephp/pull/7379

于 2015-09-13T00:33:17.360 回答
0

只需添加 Flash 组件即可。

class AppController extends Controller {

     public $components = array('DebugKit.Toolbar','Flash');

}
于 2015-10-22T15:22:24.013 回答
0

我也遇到了同样的问题,这肯定会解决你的问题。请在app/Controller/AppController.php中添加以下代码行:

public $components = array('Flash');
于 2016-03-10T06:41:19.193 回答