Reputation: 3630
How do I change the default layout globally (= for all controllers and views) in Yii2? I want to leave the main.php layout as is in case I want to use it later.
Upvotes: 10
Views: 13992
Reputation: 4129
Configure layout & layoutPath in Yii2 config:
$config = [
...
'layoutPath' => '@app/views/layouts2',
'layout' => 'main2.php',
];
Above example changes default view app/views/layouts/main.php
to app/views/layouts2/main2.php
.
Upvotes: 2
Reputation: 1
Change in config/web.php file
and add this line before components array
your new style name which is created in view/layout/main_style.php
'layout' => 'main_style'
Using this whole project layout changed
Upvotes: -1
Reputation: 2012
In root of configuration you can write default layout [[\yii\base\Application::$layout]] for all views:
[
...
'layout' => 'main',
'components' => [
...
]
]
Upvotes: 13
Reputation: 106
You can do in the following way. For example a defaultLayout.php layout can be created like this:
<?php $this->beginContent('@app/views/layouts/main.php'); ?>
<div class="container">
<div class="row">
<div class="col-lg-4">Left Side Bar</div>
<div id="content" class="col-lg-4">
<?php echo $content; ?>
</div><!-- content -->
<div class="col-lg-4">Right Side Bar</div>
</div>
</div>
<?php $this->endContent(); ?>
Inside the relative action
public function actionIndex()
{
$this->layout = 'defaultLayout';
return $this->render('index', [
'model' =>$model,
]);
}
In configuration(config/main.php) you can overwrite the default layout for all your views
[
// ...
'components' => [
'view' => [
'layout' => 'main.php'
],
// ...
],
]
Upvotes: 1
Reputation: 4076
In your config, you can edit the layoutPath .
Example:
$config = [
...
'layoutPath' => '@app/views/layouts-2'
];
Upvotes: 0