Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call to a member function success() on boolean in CakePHP

Tags:

php

cakephp

I am running into a problem with CakePHP 3.0 that does not make sense to me and would like your help solving. I am having a table name called users with a controller named the same (UsersController). I can view the users in the table without any problem, but when I insert, modify or delete a user I am getting an error.

When I do an insert I get the error message: Call to a member function success() on boolean UsersController.php on line 56

If I look into the controller class it looks like

public function add()
{
    $user = $this->Users->newEntity();
    if ($this->request->is('post')) {
        $user = $this->Users->patchEntity($user, $this->request->data);

        if ($this->Users->save($user)) {
            $this->Flash->success(__('The user has been saved.'));

            return $this->redirect(['action' => 'index']);
        } else {
            $this->Flash->error(__('The user could not be saved. Please, try again.'));
        }
    }
    $this->set(compact('user'));
    $this->set('_serialize', ['user']);
}

Line 56 is $this->Flash->success(__('The user has been saved.'));

The user is inserted, updated or deleted from the database (depending on the requested action)

What is puzzling me is why would the code return an error and most important is, how could I solve this?

Thank you very much for your time.

like image 713
Marc Witteveen Avatar asked Oct 02 '16 13:10

Marc Witteveen


1 Answers

It seems that the Flash component is not loaded in the parent class AppController. Therefor you need to either add it manually to the AppController or to your custom controller class, in my case UsersController.

In case you want to add it to the parent class AppController open the AppController file and add the following PHP code snipper in side of the class.

public function initialize()
{
    $this->loadComponent('Flash');
}

Incase you want to load the Flash component only in your custom class then add the following code snipper inside your custom class.

public function initialize()
{
    parent::initialize();
    $this->loadComponent('Flash');
}

This will make the Flash component available to you and removed the error as described in the initial post.

like image 116
Marc Witteveen Avatar answered Sep 27 '22 23:09

Marc Witteveen