首页 文章

Yii2中某些页面的布局相同

提问于
浏览
0

我是yii的新手 . 我有一个名为main.php的页面 . 我希望其他一些页面使用main.php作为布局 . 我的项目视图位于“site”文件夹中 . 我为那些视图和一个单独的控制器创建了一个文件夹 . 但它不起作用 . 我将$ this-> layout ='main'添加到我的项目中

3 回答

  • 1

    main.php 或任何布局文件放在 views/layouts 文件夹中 .

    在控制器中使用它: public $layout = '/main';

    或者在行动中: $this->layout = '/main';

  • 0

    在你的Yii2脚手架中应该有一个目录

    your_application /视图/布局

    把布局文件main.php放在这个目录中

    (或编辑或替换现有的)

    一旦你在需要新布局的controllerAction中完成了布局

    $this->layout = 'main';
    

    .

    class YourController extends Controller
    {
        ....
        public function actionYourAction()
        {
            .......
    
            $this->layout = 'main';
            return $this->render( ....        ]);
        }
    
  • 0

    要更改 ALL 控制器和操作,您必须在 config/main.php 文件中添加:

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

    改变一个控制器:

    class SiteController extends Controller
    
    {
    
        public $layout='//layouts/main';
    
        public function init() {
            // ...
        }
        //...
    }
    

    只需一个动作即可更改:

    public function actionIndex()
    {
        $this->layout = 'mian';
    
        return $this->render('index', ['model' =>$model]);
    }
    

相关问题