Php 无法通过构造函数设置布局
我几乎什么都试过了,但我就是无法运行下面的程序Php 无法通过构造函数设置布局,php,laravel,laravel-4,Php,Laravel,Laravel 4,我几乎什么都试过了,但我就是无法运行下面的程序 <?php class BaseController extends Controller { // Define frontpage layout manager protected $layout = ''; public function __construct() { parent::__construct(); $theme = Theme::where('enabled', '=', true)
<?php
class BaseController extends Controller {
// Define frontpage layout manager
protected $layout = '';
public function __construct() {
parent::__construct();
$theme = Theme::where('enabled', '=', true)->first();
// HERE !! : This never changes the value of $layout class var
$this->layout = View::make('themes.front.' . $theme->folder . '.master');
// I also tried without View::make(..)
// I also checked that $theme returns something and it does return a theme
}
/**
* Setup the layout used by the controller.
*
* @return void
*/
protected function setupLayout()
{
if ( ! is_null($this->layout))
{
$this->layout = View::make($this->layout);
}
}
}
因此,我想要实现的是:我将有多个布局(模板),我将允许用户通过管理更改这些模板,因此我需要一种快速简便的方法来操作受保护的$layout
值
将我的代码放入\uu constructor(){}
的问题是setupLayout()
将覆盖它,因此当找不到布局时会出现错误
因此有两种解决方案:
1)声明每个子控制器中的布局
这意味着扩展基本控制器的每个控制器都在自己的\uuu构造函数(){}
方法中定义自己的受保护的$layout
。但是,如果您的所有页面共享同一个模板,那么这是非常重复的
2)手动上传setupLayout()方法
由于我的所有页面都共享相同的布局,而且我知道certian始终至少有一个模板,因此我可以简单地将setupLayout()
方法更改为:
function setupLayout()
{
$theme = Theme::where('enabled', '=', true)->first();
$this->layout = 'themes.front.' . $theme->folder . '.master';
}
有错误吗?它应该没有View::make
。仅$this->layout='themes.front'$主题->文件夹“”。主人'代码>