Cakephp 如何为常规authError消息定义FlashHelper/Component元素

Cakephp 如何为常规authError消息定义FlashHelper/Component元素,cakephp,cakephp-2.6,cakephp-2.7,Cakephp,Cakephp 2.6,Cakephp 2.7,在将CakePHP从2.6.2更新到2.7.2之后,当创建auth flash消息时,我得到一个缺少密钥的错误。如何为默认的authError定义元素模板 自从SessionComponent::setFlash()被添加到app/Controller/AppController.php中以来,我修改了所有来自以下位置的Flash消息: //控制器 $this->Session->setFlash('Done','success'); $this->Session->setFlash(“出现错误

在将CakePHP从2.6.2更新到2.7.2之后,当创建auth flash消息时,我得到一个缺少密钥的错误。如何为默认的
authError
定义元素模板

自从
SessionComponent::setFlash()
被添加到
app/Controller/AppController.php
中以来,我修改了所有来自以下位置的Flash消息:

//控制器
$this->Session->setFlash('Done','success');
$this->Session->setFlash(“出现错误”,“失败”);
$this->Session->setFlash('请登录','验证');
//视图(默认布局)
echo$this->Session->flash();
echo$this->Session->flash('auth');
为此:

//控制器
$this->Flash->success('Done');
$this->Flash->failure('有一个错误');
$this->Flash->auth('请登录');
//视图(默认布局)
echo$this->Flash->render();
echo$this->Session->flash();//暂时保留?
echo$this->Session->flash('auth');//暂时保留?
我还从中复制了与flash相关的模板
App/View/Elements/success.ctp
to
App/View/Elements/Flash/success.ctp

这是有效的–但是如果我未登录并尝试访问管理页面,我会在
app/Controller/AppController.php
中显示默认消息,而不显示相应的模板。使用调试模式2时,我出现以下错误:

//未定义变量:key[CORE\Cake\View\Elements\Flash\default.ctp,第1行]
//包括-CORE\Cake\View\Elements\Flash\default.ctp,第1行
//视图::_evaluate()-CORE\Cake\View\View.php,第971行
//视图::_render()-CORE\Cake\View\View.php,第933行
//视图::_renderElement()-CORE\Cake\View\View.php,第1227行
//View::element()-CORE\Cake\View\View.php,第418行
//SessionHelper::flash()-CORE\Cake\View\Helper\SessionHelper.php,第159行
//包括-APP\View\Layouts\default.ctp,第142行
//视图::_evaluate()-CORE\Cake\View\View.php,第971行
//视图::_render()-CORE\Cake\View\View.php,第933行
//View::renderLayout()-CORE\Cake\View\View.php,第546行
//View::render()-CORE\Cake\View\View.php,第481行
//Controller::render()-CORE\Cake\Controller\Controller.php,第960行
//Dispatcher::_invoke()-CORE\Cake\Routing\Dispatcher.php,第200行
//Dispatcher::Dispatcher()-CORE\Cake\Routing\Dispatcher.php,第167行
//[main]-APP\webroot\index.php,第118行
//消息“class=”消息“>
要使用我自己的元素模板“auth”呈现默认的authError,需要对AppController.php进行哪些更改

下面是AppController.php的一部分:

public$components=array(
"闪光",,
“会议”,
“安全”,
'Auth'=>数组(
'authenticate'=>array('Form'=>array('passwordHasher'=>'Blowfish')),
'authError'=>'我的默认身份验证错误消息',//我必须如何修改此行?
'loginAction'=>array('controller'=>'users','action'=>'login'),
'LoginDirect'=>array('controller'=>'users','action'=>'welcome'),
'logoutRedirect'=>array('controller'=>'users','action'=>'debye'),
)
);
当将所有控制器中的所有flash消息更改为flash组件和helper时,这两行代码是否仍然是必需的?CakePHP还使用了哪两行代码

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

我还查看了。但它似乎不是最新的,因为
$this->Session->setFlash()
仍在大量使用中…

在您的Auth组件设置数组中添加如下内容

'Auth' = [
    ...
    'flash' => ['element' => 'auth_error'],
    ...
]
然后在
元素/Flash
目录中创建名为
auth_error.ctp
的模板。在此文件中,您使用的唯一变量应该是
$message
,因为当cake从auth组件调用Flash时,不会传递任何其他变量(即
$key
变量)


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

这是蛋糕本身的一个缺陷,它(将)在2.7.4中修复


请参阅:

只需添加Flash组件即可工作

class AppController extends Controller {

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

}

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

public $components = array('Flash');

我遇到以下错误:错误:调用成员函数success()在非-object@DariusVE在我的代码示例中,我使用了
success
作为模板名,很明显您使用了
success
。因此,应该通过添加模板
App/View/Elements/Flash/success.ctp
来修复错误。昨天我解决了这个问题,您需要在AppController.php
public$components=array('Session','Flash','Auth');