Call function member function () in boolean in CakePHP

I have a problem with CakePHP 3.0 which does not make sense to me and would like your help to be resolved. I have a table name called users with a controller named same (UsersController). I can view users in a table without any problems, but when I insert, modify or delete a user, I get an error.

When I do the insert, I get an error: call to function member () function in boolean UsersController.php on line 56

If I look at 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 (__ ('User was saved.'));

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

What puzzles me is why the code will return an error and, most importantly, how can I solve this?

Thanks so much for your time.

+5
source share
1 answer

It seems that the Flash component is not loaded in the parent class of the AppController. Therefore, you need to either add it manually in the AppController, or in your own controller class, in my case UserController.

If you want to add it to the parent class of the AppController, open the AppController file and add the following PHP block reader to the class side.

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

If you want to load the Flash component only into your custom class, add the following line of code to your own class.

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

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

+6
source

Source: https://habr.com/ru/post/1257614/


All Articles