问题
I'm new with cake sorry if this is a simple problem.
When i finish to save the data and i try to show a message with information about if the user is saved or could not saved , show me the next error :
Error: Call to a member function error() on a non-object File C:\wamp\www\proyecto\src\Controller\AdministradorsController.php Line: 76
AdministradorsController extends AppController
public function add()
{
$administrador = $this->Administradors->newEntity();
if ($this->request->is('post')) {
$this->loadModel('Personas');
$persona = $this->Personas->newEntity();
$persona->rut = $this->request->data['Personas']['rut'];
$persona->sexo = $this->request->data['Personas']['sexo'];
$persona->nombre = $this->request->data['Personas']['nombre'];
$persona->apellido_paterno = $this->request->data['Personas']['apellido_paterno'];
$persona->apellido_materno = $this->request->data['Personas']['apellido_materno'];
$persona->direccion = $this->request->data['Personas']['direccion'];
$persona->telefono_fijo = $this->request->data['Personas']['telefono_fijo'];
$persona->telefono_movil = $this->request->data['Personas']['telefono_movil'];
$persona->fecha_nacimiento = $this->request->data['Personas']['fecha_nacimiento'];
$persona->email = $this->request->data['Personas']['email'];
$persona->comuna_id = $this->request->data['Personas']['comuna_id'];
if(!$this->Personas->save($persona)){
$this->Flash->error('The administrador could not be saved. Please, try again.');
}
$administrador = $this->Administradors->newEntity();
$administrador->persona_id = $persona->id;
if(!$this->Administradors->save($administrador)){
$this->Flash->error('The administrador could not be saved. Please, try again.');
}
$this->loadModel('Users');
$user = $this->Users->newEntity();
$user->username = $persona->email;
$user->password = $this->rand_passwd(6);
$user->estado = true;
$user->persona_id = $persona->id;
$user->role_id = ADMIN;
if($this->Users->save($user)){
$this->Flash->success('The administrador has been saved.');
return $this->redirect(['action' => 'index']);
}else{
$this->Flash->error('The administrador could not be saved. Please, try again.');
}
}
$personas = $this->Administradors->Personas->find('list', ['limit' => 200]);
$this->set(compact('administrador', 'personas'));
$this->set('_serialize', ['administrador']);
$this->loadModel('Regions');
$comunas = $this->Regions->Comunas->find('list', ['limit' => 200]);
$this->set(compact('comuna', 'comunas'));
$this->set('_serialize', ['comuna']);
}
The data is inserting into the data base but the Flash message give me a error, sorry for my poor english and thx
回答1:
If you are loading the Flash
component in your AppController
, the issue could be that you have the method initialize()
in your AdministratorsController
and you are not calling parent::initialize();
in that method. It is a must for it to work so all the helpers
, components
, etc, that are initialized in AppController
s initialize()
method are transferred over.
回答2:
Rendering Flash Messages in cakephp 3
In controller:
<?php
$this->Flash->error(__('Your error message'), ['key' => 'error']);
?>
In View:
<?= $this->Flash->render('error'); ?>
In
src/Template/Element/Flash/success.ctp
change the div class toclass="alert alert-success"
i.e.
<?php
if (!isset($params['escape']) || $params['escape'] !== false) {
$message = h($message);
}
?>
<div class="alert alert-success" onclick="this.classList.add('hidden')"><?= $message ?></div>
Apply the same class to
src/Template/Element/Flash/error.ctp
回答3:
to ease the work with the Flash Messages you can use a simple notification plugin turbo-tribble
来源:https://stackoverflow.com/questions/29595300/flash-message-in-cakephp-3-it-doesnt-working