shamim
shamim

Reputation: 47

same layout for some pages in Yii2

I am a newbie at yii . I have a page called main.php . I want some of the other pages to use main.php as layout. my project views are in "site" folder. I create a folder for that views and a separated controller. but it doesn't work.I add $this->layout = 'main' to my project

Upvotes: 0

Views: 335

Answers (3)

Alireza Fallah
Alireza Fallah

Reputation: 4607

For changing for ALL controllers and actions you must add this in your config/main.php file:

[
     // ...
     'layout' => 'main',
     'components' => [
         //...
     ]
]

Change in one controller:

class SiteController extends Controller

{

    public $layout='//layouts/main';

    public function init() {
        // ...
    }
    //...
}

Change just in one action:

public function actionIndex()
{
    $this->layout = 'mian';

    return $this->render('index', ['model' =>$model]);
}

Upvotes: 0

Put your main.php or any layout files in views/layouts folder.

Use this in controllers: public $layout = '/main';

Or in actions: $this->layout = '/main';

Upvotes: 1

ScaisEdge
ScaisEdge

Reputation: 133380

In your Yii2 scaffolding should be there a directory

your_application/views/layouts

put the layout file main.php in this dir

(or edit or replace the existing one)

once you have done in the controllerAction where you need the new layout assign the layout

$this->layout = 'main';

.

class YourController extends Controller
{
    ....
    public function actionYourAction()
    {
        .......

        $this->layout = 'main';
        return $this->render( ....        ]);
    }

Upvotes: 0

Related Questions