3

I am using CakePHP 2.6 and trying to follow along with the simple authentication tutorial. I am using another model Account for my Auth->User. After adding in the Flash component in my AppController - I'm seeing the error message on all of my pages:

Error: FlashComponent could not be found.

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

<?php
class FlashComponent extends Component {

}

Now I know that I currently do not have a FlashComponent.php file in app\Controller\Component, am I supposed to actually add it there? I am not seeing anything in the tutorial about it.

Thank you!

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(); ?>
Inigo Flores
  • 4,461
  • 1
  • 15
  • 36
user2836292
  • 305
  • 2
  • 5
  • 13

2 Answers2

7

The FlashComponent was added to CakePHP in v2.7. For prior versions you need to use SessionComponent and use $this->Session->flash() in your controller to set flash messages.

ADmad
  • 8,102
  • 16
  • 18
  • Wow...simple enough. I just upgraded to 2.7 then (using 2.6 through Bitnami). Thanks you! – user2836292 Dec 04 '15 at 13:10
  • I wasn't able to upgrade, so I had to use the `SessionComponent` and can explain it a bit further in case anyone else needs legacy code samples. In your controller, ensure that you have `"Session"` in your `$helpers` array, and set flash messages via `$this->Session->setFlash('message here');`. In your view, render them via `echo $this->Session->flash();` (hence the need for the [Session Helper](http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#displaying-notifications-or-flash-messages) in your controller). – Sam Mar 02 '16 at 16:36
0

$this->Session->setFlash if you are using cakephp 2.5.3.