CAKEPHP 3 - 在组件内部使用后无法识别 Flash 组件

CAKEPHP 3 - Flash component unrecognized after used inside component

我已经编写了一个组件 ( isPilotFree ) 来进行一些检查。

<?php

namespace App\Controller\Component;

use Cake\Controller\Component;
use Cake\Controller\Controller;

class SharedComponent extends Component
{
    public $components = [ 'Flash' ];

    /** check any pilot overlap
    *
    * @param string|null booking record
    * @return true / false
    **/

    public function pilotIsFree( $booking, $who )
    {
        $controller = $this->_registry->getController();

        $Bookings = $controller->Bookings;

        $res = true;

        if( $booking->id == null ) 
            $booking_id = -1;
        else
            $booking_id = $booking->id;

        if( $res )
        {
            $bookings =  $Bookings->find( 'all' )->Where( [ 'provider_pk != ' =>  $booking->provider_pk, 'Bookings.id !=' => $booking_id, $who, 'start = ' => $booking->start ] );

            $res = $bookings->count() == 0;
        }

        // deleted some lines for simplicity
        ... ... ...             

        if( !$res ) 
        {
            $this->Flash->error( __('The pilot is not available.') );
        }

        return( $res );
    }

}
?>

此组件从控制器内部调用为:

        if( $this->Shared->pilotIsFree( $booking, [ 'Bookings.requester_pk =' => $booking->requester_pk ] )  )
        {
          ... ... ..
        }

组件在控制器中加载为:

/**
 * Initialization method
 *
 * @return none
 */
public function initialize()
{
    $this->loadComponent( 'Shared' );
}

我现在遇到的问题是组件内部的 Flash 调用工作正常,但是当我尝试从控制器内部触发 Flash 消息时,出现了这个错误:

错误:在布尔值上调用成员函数 success() 文件 .../src/Controller/BookingsController.php 行:191

我发现让它正常工作的唯一方法是在控制器的开头添加这个:

public $组件 = [ 'Flash' ];

不应在父 class AppController 中加载 Flash 组件。实际上,如果我不加载/使用我的自定义组件,我可以从控制器内部触发 Flash 消息,而无需添加上一行。

我错过了什么吗?

谢谢。 问候。 法昆多.

您需要将其放入您的控制器中。

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

或在没有 parent::initialize();

的 AppController 中