Domanda

I want to dynamically set the layout from an application wide parameter set in the config file.

I thought something like the following would work:

<?php

class SiteController extends Controller
{
    public $layout;
    public $layout_name;
    $this->layout_name = Yii::app()->params['layout'];

    $this->layout = "//layouts/".$this->layout_name;

I've become stuck, I think owing to a fundamental mis-understanding of PHP [to be completely honest]. I get the following error:

Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in /chroot/home/mikloswe/miklos.web44.net/html/content/protected/controllers/SiteController.php on line 7

Can anyone see a way around this or point out a better alternative to setting the layout file? My chief objective is to set the layout file in just one place to cover all controllers.

Also, I wouldn't mind an explanation of why PHP doesn't allow me to put Yii::app()->params['layout'] outside of a function but within a class file, I feel like I'm missing something.

È stato utile?

Soluzione

Yii has the init function meant for these kinds of things. Add this method to your class;

public function init() {
    $this->layout_name = Yii::app()->params['layout'];

    $this->layout = "//layouts/".$this->layout_name;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top