• <i id='p9Xna'><tr id='p9Xna'><dt id='p9Xna'><q id='p9Xna'><span id='p9Xna'><b id='p9Xna'><form id='p9Xna'><ins id='p9Xna'></ins><ul id='p9Xna'></ul><sub id='p9Xna'></sub></form><legend id='p9Xna'></legend><bdo id='p9Xna'><pre id='p9Xna'><center id='p9Xna'></center></pre></bdo></b><th id='p9Xna'></th></span></q></dt></tr></i><div id='p9Xna'><tfoot id='p9Xna'></tfoot><dl id='p9Xna'><fieldset id='p9Xna'></fieldset></dl></div>

    <small id='p9Xna'></small><noframes id='p9Xna'>

    • <bdo id='p9Xna'></bdo><ul id='p9Xna'></ul>
    <legend id='p9Xna'><style id='p9Xna'><dir id='p9Xna'><q id='p9Xna'></q></dir></style></legend>
    <tfoot id='p9Xna'></tfoot>

      1. Yii 检查主页

        时间:2023-09-24

        <small id='Scymq'></small><noframes id='Scymq'>

        • <tfoot id='Scymq'></tfoot>

          <legend id='Scymq'><style id='Scymq'><dir id='Scymq'><q id='Scymq'></q></dir></style></legend>
              <tbody id='Scymq'></tbody>
              <bdo id='Scymq'></bdo><ul id='Scymq'></ul>

            • <i id='Scymq'><tr id='Scymq'><dt id='Scymq'><q id='Scymq'><span id='Scymq'><b id='Scymq'><form id='Scymq'><ins id='Scymq'></ins><ul id='Scymq'></ul><sub id='Scymq'></sub></form><legend id='Scymq'></legend><bdo id='Scymq'><pre id='Scymq'><center id='Scymq'></center></pre></bdo></b><th id='Scymq'></th></span></q></dt></tr></i><div id='Scymq'><tfoot id='Scymq'></tfoot><dl id='Scymq'><fieldset id='Scymq'></fieldset></dl></div>

                  本文介绍了Yii 检查主页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  Yii 中是否有内置方法或属性来检查页面是否为主页?

                  我知道我可以使用这样的东西:

                  $controller = Yii::app()->getController();$isHome = $controller->getAction()->getId() === 'index' ?真假;

                  或者把它放在主控制器的一个方法中,但我正在寻找更干净的东西.

                  谢谢.

                  解决方案

                  如果要查看当前页面,即 action 是当前控制器的默认..

                  $controller = Yii::app()->getController();$isHome = $controller->action->id === $controller->defaultAction->id ?真假;

                  <块引用>

                  dafeultaction 可能并不总是'index',它可以改变,所以你需要将它与 defaultAction 进行比较..

                  如果你的意思是网站的默认页面,那么你需要将你的控制器与 defaultController.. 进行比较.

                  $controller = Yii::app()->getController();$default_controller = Yii::app()->defaultController;$isHome = (($controller->id === $default_controller->id) && ($controller->action->id === $controller->defaultAction->id)) ?真假;

                  在 Yii2 中:

                  $controller = Yii::$app->controller;$default_controller = Yii::$app->defaultRoute;$isHome = (($controller->id === $default_controller) && ($controller->action->id === $controller->defaultAction)) ?真假;

                  Is there a buildin method or property in Yii to check if page is homepage?

                  I know i can use something like this:

                  $controller = Yii::app()->getController();
                  $isHome = $controller->getAction()->getId() === 'index' ? true : false;
                  

                  Or put it in a method in main controller, but i am looking for something cleaner.

                  Thanks.

                  解决方案

                  If You want to check the current page, ie action is the default of the current controller..

                  $controller = Yii::app()->getController();
                  $isHome = $controller->action->id === $controller->defaultAction->id ? true : false;
                  

                  dafeultaction may not always be 'index', it can be changed, so you need to compare it with defaultAction instead..

                  And by homepage if you mean the defult page of site, then you need to compare your controller also with defaultController..

                  $controller = Yii::app()->getController();
                  $default_controller = Yii::app()->defaultController;
                  $isHome = (($controller->id === $default_controller->id) && ($controller->action->id === $controller->defaultAction->id)) ? true : false;
                  

                  In Yii2:

                  $controller = Yii::$app->controller;
                  $default_controller = Yii::$app->defaultRoute;
                  $isHome = (($controller->id === $default_controller) && ($controller->action->id === $controller->defaultAction)) ? true : false;
                  

                  这篇关于Yii 检查主页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:带有复合主键的 Yii 模型 下一篇:Yii CDbCriteria 加入

                  相关文章

                  <small id='1koIr'></small><noframes id='1koIr'>

                    1. <legend id='1koIr'><style id='1koIr'><dir id='1koIr'><q id='1koIr'></q></dir></style></legend>
                        <bdo id='1koIr'></bdo><ul id='1koIr'></ul>
                    2. <tfoot id='1koIr'></tfoot>
                    3. <i id='1koIr'><tr id='1koIr'><dt id='1koIr'><q id='1koIr'><span id='1koIr'><b id='1koIr'><form id='1koIr'><ins id='1koIr'></ins><ul id='1koIr'></ul><sub id='1koIr'></sub></form><legend id='1koIr'></legend><bdo id='1koIr'><pre id='1koIr'><center id='1koIr'></center></pre></bdo></b><th id='1koIr'></th></span></q></dt></tr></i><div id='1koIr'><tfoot id='1koIr'></tfoot><dl id='1koIr'><fieldset id='1koIr'></fieldset></dl></div>