cakephp 3 中的 Flash 消息不起作用

Flash Message in cakephp 3 it doesn't working

我是蛋糕新手,抱歉,如果这是一个简单的问题。

当我完成保存数据并尝试显示一条消息,其中包含有关用户是已保存还是无法保存的信息时,请显示下一个错误:

错误:调用非对象的成员函数 error() 文件 C:\wamp\www\proyecto\src\Controller\AdministradorsController.php 行: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']);
}

数据正在插入数据库,但 Flash 消息给我一个错误,抱歉我的英语不好,谢谢

如果您在 AppController 中加载 Flash 组件,问题可能是您的 AdministratorsController 中有方法 initialize() 而您没有调用parent::initialize(); 在该方法中。它必须工作,所以在 AppControllers initialize() 方法中初始化的所有 helperscomponents 等都被转移。

为了简化 Flash 消息的工作,您可以使用一个简单的通知插件 turbo-tribble

Rendering Flash Messages in cakephp 3

在控制器中:

<?php
    $this->Flash->error(__('Your error message'), ['key' => 'error']);
?>

在视图中:

<?= $this->Flash->render('error'); ?>

In src/Template/Element/Flash/success.ctp change the div class to class="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