[英]How to keep the main page as “index.php” while still Requiring log-in on other pages - yii
I'm running into some difficulty in using the added componenet requirelogin.php . 使用添加的componenet requirelogin.php时遇到了一些困难。 This is the component that I've added
这是我添加的组件
<?php
class RequireLogin extends CBehavior
{
public function attach($owner)
{
$owner->attachEventHandler('onBeginRequest', array($this, 'handleBeginRequest'));
}
public function handleBeginRequest($event)
{
if (Yii::app()->user->isGuest && !in_array($_GET['r'],array('site/login', 'site/index'))) {
Yii::app()->user->loginRequired();
}
}
}
?>
Note how 'site/index' is allowed in this as a page that I can visit. 请注意,在此页面中,我可以访问“站点/索引”的方式。
Now, in my main.php I added the following 现在,在我的main.php中,添加了以下内容
'behaviors' => array(
'onBeginRequest' => array(
'class' => 'application.components.RequireLogin'
)
),
Now, these two force me to go to site/login everytime - even though I have done as other stackoverflow answers have told me to and added 现在,这两个命令迫使我每次都去站点/登录-尽管我已经按照其他stackoverflow答案告诉我的那样进行了操作并添加了
// sets the default controller action as the index page
'defaultController' => 'site/index',
Could anyone explain why this hasn't made my start page site/index? 谁能解释为什么这没有使我的起始页网站/索引成功?
_____________ ADDITION _____________添加
I've also figured out that when I am going to the base action (ie mywebsite.com) it is NOT going to site/index. 我还发现,当我要执行基本操作(即mywebsite.com)时,它不会转到网站/索引。 rather it is directly redirecting to site/login.
而是直接重定向到站点/登录。 This, however, does not occur, when I comment out the behaviors .
但是,当我注释掉这些行为时,不会发生这种情况。
So, thank you darkheir for all of your work on this. 因此,感谢Darkheir在此方面所做的所有工作。 Ultimately, what happened was the $_GET['r] in the base path was simply '' because r was not point to anything in particular.
最终,发生的是基本路径中的$ _GET ['r]只是因为”,因为r没有特别指向任何东西。 As a result, when ever I looked in_array($_GET['r']) what I was getting in the very bae path was in_array('', array('site/login', 'site/account')) Now, unfortunately, '' is neither site/login or site/account so the page redirected using
结果,每当我查看in_array($ _ GET ['r'])时,我在bae路径中得到的就是in_array('',array('site / login','site / account'))现在,不幸的是,“”既不是网站/登录名也不是网站/帐户,因此该页面使用
Yii::app()->user->loginRequired();
The fix to this problem was 解决此问题的方法是
public function handleBeginRequest($event)
{
// note that '' is now one of the options in the array
if (Yii::app()->user->isGuest && !in_array($_GET['r'],array('site/login', 'site/index', '' ))) {
Yii::app()->user->loginRequired();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.