【问题标题】:cakephp2: how to set layout for cake exceptioncakephp2:如何为蛋糕异常设置布局
【发布时间】:2012-12-05 21:05:55
【问题描述】:

我遇到了一个相当复杂的情况来渲染错误页面 1) 蛋糕异常 NotFoundException 是从插件中抛出的; 2)我想从应用程序而不是从这个插件呈现布局;

我尝试设置 $this->layout='default';在插件控制器的 beforeFilter 方法中,但仍然呈现插件布局而不是应用布局。

我查看了 CakeErrorController,但没有找到布局设置的位置。

知道如何管理吗?

【问题讨论】:

标签: cakephp layout plugins


【解决方案1】:

我不确定这是否对你有用,但这就是我所做的。我(在我的 AppController 中)将其设置在 __construct() 函数中:

    function __construct() {

        parent::__construct();

        if ($this->name == 'CakeError') { 
            $this->layout = 'default';
            $this->constructClasses();


            $this->beforeFilter();

            $this->beforeRender();
        }   
    }

同样,您的方法可能会有所不同,但这是我的实现。

此外,您可以在定义的错误处理程序中使用 AppError.php 文件进行设置。请注意,我的方法是针对 Cake 1.3,所以我不确定 Cake2 中的 AppError 文件有何不同。

<?php
class AppError extends ErrorHandler {


function error403($params) {
    extract($params, EXTR_OVERWRITE);

    if (!isset($url)) {
        $url = $this->controller->here;
    }

    $this->controller->helpers = array_merge( $this->controller->helpers, array( 'Asset.Asset', 'Auth' ) );

    $url = Router::normalize($url);


    $this->controller->set(array(
        'code' => '403',
        'name' => __('Permission Denied', true),
        'message' => $message,
        'base' => $this->controller->base
    ));


    $this->_outputMessage('error403');

    }   
}

【讨论】:

  • 最后的、hacky、可能可行的方法是在错误视图文件中设置$this-&gt;layout = 'yourlayout'。
猜你喜欢
  • 2014-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-10
  • 1970-01-01
  • 1970-01-01
  • 2016-06-15
  • 1970-01-01
相关资源
最近更新 更多