yii2-更改默认显示的通用主页

在views/layouts/目录下新建一个login.php,然后SiteController中更新下面的方法

public function actionIndex()
    {      
        $this->layout = 'login';        // 设置通用的模版为views/layouts/login.php
        return $this->render('login'); //render()会将views/site/login.php拼接到通用模版中
    }

原理在于:
在yiiaseController中存在该函数

    public function findLayoutFile($view)
    {
        $module = $this->module;   //如果是web应用,该处的module是appwebApplication
        if (is_string($this->layout)) {
            $layout = $this->layout;   //如果设置了layout属性则$this->layout的值将作为最终的layout值
        } elseif ($this->layout === null) {
            while ($module !== null && $module->layout === null) {
                $module = $module->module;  
            } 
            if ($module !== null && is_string($module->layout)) {
                $layout = $module->layout; //appwebApplication->layout 继成于appaseApplication->layout,默认值为main,如果没有设置layout属性则返回应用默认的layout
            }
        }

        if (!isset($layout)) {
            return false;
        }

        if (strncmp($layout, '@', 1) === 0) {
            $file = Yii::getAlias($layout);
        } elseif (strncmp($layout, '/', 1) === 0) {
            $file = Yii::$app->getLayoutPath() . DIRECTORY_SEPARATOR . substr($layout, 1); 
        } else {
            $file = $module->getLayoutPath() . DIRECTORY_SEPARATOR . $layout;
        }

        if (pathinfo($file, PATHINFO_EXTENSION) !== '') {
            return $file;
        }
        $path = $file . '.' . $view->defaultExtension;
        if ($view->defaultExtension !== 'php' && !is_file($path)) {
            $path = $file . '.php';
        }

        return $path;
    }

因为SiteController->yiiwebController->yiiaseController,因此只需要在该类的layout成员变量即可达到如下效果。
这里写图片描述
默认为登录页面,页面的样式可以随意更改
这里写图片描述
登录成功之后的样式为左右两列

版权声明:本文为博主原创文章,未经博主允许不得转载。

如果我能一直坚持下去,就像坚持当初的梦想一样,或许我也能成为大神呢,哈哈哈。
原文地址:https://www.cnblogs.com/faraway-tiny/p/4918354.html