【问题标题】:Force CakePHP plugin to use its own layout强制 CakePHP 插件使用自己的布局
【发布时间】:2017-11-16 06:40:45
【问题描述】:

我写了一个 CakePHP 插件https://github.com/anuj9196/CakePHP-App-Installer

插件使用来自plugin_path/src/template/layout/default.ctpdefault.ctp布局

当宿主应用程序中使用了其他主题时。就像在我的情况下,我在AppControllerbeforeRender() 中设置了一个

$this->viewBuilder()->setTheme('DashboardTemplate');

DashboardTemplate 在应用程序的/plugin/ 目录中。

现在,当我使用 example.com/installer/install 访问插件的 URL 时

模板加载在DashboardTemplate 主题之上。

如何在插件的 AppController 中禁用它们?

插件目录内的AppController包含

<?php
namespace Installer\Controller;

use App\Controller\AppController as BaseController;

class AppController extends BaseController
{
    // nothing here
}

【问题讨论】:

    标签: cakephp cakephp-3.4


    【解决方案1】:

    在插件的AppController 中使用beforeRender() 删除主题。

    <?php
    namespace Installer\Controller;
    
    use App\Controller\AppController as BaseController;
    
    class AppController extends BaseController
    {
        /**
         * @param \Cake\Event\Event $event The beforeRender event.
         * @return \Cake\Http\Response|null|void
         */
        public function beforeRender(Event $event)
        {
             try {
                 return parent::beforeRender($event);
             } finally {
                 $this->viewBuilder()->setTheme(null);
             }
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以相当轻松地在视图和控制器中的布局之间切换.. 使用插件语法

      // inside controller 
        $this ->layout = 'Plugin.layout';
      
      //inside view template
          $this ->layout = 'Plugin.layout';
      

      如果您只想禁用主题,请使用上述 Mathew 的方法。但请注意,如果您的某些应用程序代码在您的插件之后运行,则会禁用整个应用程序的主题,而不仅仅是这个插件

      【讨论】:

      • 很确定您在此处显示的语法适用于 CakePHP 的某些早期版本,而不是 3.x 系列。
      猜你喜欢
      • 2013-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-23
      • 1970-01-01
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      相关资源
      最近更新 更多