CakePHP 3组件在一个单元格中

问题描述 投票:0回答:2

我创建了自己的CakePHP 3组件。我使用$this->loadComponent()将它包含在一个控制器中,它工作正常;但是,我还需要将它包含在一个没有loadComponent()方法的Cell中。

如何在单元格中包含组件?

cakephp cakephp-3.0
2个回答
2
投票

你不在那里使用组件。因为单元格是单元格而不是控制器。一个单元甚至不知道控制器,检查源。 http://api.cakephp.org/3.3/source-class-Cake.View.Cell.html

如果您需要/想要以任何方式使用单元格内的组件,我认为您的架构设计错误。由于您“忘记”共享代码,因此无法提供任何进一步的建议。无论您在那里尝试做什么,都要重构您的应用程序架构。


0
投票

不会说这是最佳做法,但如果您需要从单元格访问组件,这里是一个快速的解决方法。

在你的AppController中

use Cake\Core\Configure;

class PageController extends AppController
{
    public function initialize()
    {
        Configure::write('controller', $this);
    }
}

在你的细胞中

<?php
namespace App\View\Cell;

use Cake\Core\Configure;

class ExampleCell extends Cell
{
    public function getController(){
        return Configure::read('controller');
    }
    public function display()
    {
        $data = $this->getController()->ComponentName->method();
        $this->set('data', $data);
    }

}

© www.soinside.com 2019 - 2024. All rights reserved.