CakePHP:找不到 FlashComponent

CakePHP: FlashComponent could not be found

我正在使用 CakePHP 2.6 并尝试按照简单的身份验证教程进行操作。我正在为我的 Auth->User 使用另一个模型 Account。在我的 AppController 中添加 Flash 组件后 - 我在所有页面上都看到错误消息:

Error: FlashComponent could not be found.

Error: Create the class FlashComponent below in file: app\Controller\Component\FlashComponent.php

<?php
class FlashComponent extends Component {

}

现在我知道我目前在 app\Controller\Component 中没有 FlashComponent.php 文件,我真的应该把它添加到那里吗?我在教程中没有看到任何关于它的内容。

谢谢!

AppController

    public $components = array(
    'Flash',
    'Auth' => array(
        'loginRedirect' => array(
            'controller' => 'accounts',
            'action' => 'index'
        ),
        'loginAction' => array(
            'controller' => 'accounts',
            'action' => 'login'
        ),
        'logoutRedirect' => array(
            'controller' => 'accounts',
            'action' => 'login',
        ),
        'authenticate' => array('Form' => array(
                    'userModel' => 'Account',
                    'passwordHasher' => 'Blowfish',
                     'fields' => array(
                                       'username' => 'email',
                                       'password' => 'token',
                                       )
                   )
        )
    )
);

public function beforeFilter() {
    $this->Auth->allow('index', 'view');
}

Login.ctp

<?php echo $this->Flash->render('auth'); ?>
<?php echo $this->Form->create('Account', array('action' => 'login')); ?>
<?php echo $this->Form->input('email', array('class' => 'form-control', 'type' => 'email', 'placeholder' => 'Email', 'label' => false)); ?>    
<?php echo $this->Form->input('token', array('class' => 'form-control', 'type' => 'password', 'placeholder' => 'Password', 'label' => false)); ?> 
<?php echo $this->Form->submit('Sign In', array('class' => 'btn btn-primary btn-block btn-flat')); ?>
<?php echo $this->Form->end(); ?>

FlashComponent 已在 v2.7 中添加到 CakePHP。对于以前的版本,您需要在控制器中使用 SessionComponent 并使用 $this->Session->flash() 来设置快闪消息。

$this->Session->setFlash 如果您使用的是 cakephp 2.5.3.