牛骨文教育服务平台(让学习变的简单)
博文笔记

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

创建时间:2015-07-03 投稿人: 浏览次数:1934

在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成员变量即可达到如下效果。
这里写图片描述
默认为登录页面,页面的样式可以随意更改
这里写图片描述
登录成功之后的样式为左右两列

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。