首页 文章

Yii url经理无限重定向循环

提问于
浏览
0

我坚持这个......

我有一个Yii项目(Yii 1.1.14),它在我的本地机器上运行正常(IIS 7.5,ISAPI重写3,Php 5.5) .

然而,在测试服务器上(Linux,Apache 2.2,Php 5.5),一切都有效,除了重定向 . 我在主页上获得了无限重定向循环,因为login-和index-controllers正在相互重定向(这根本不会发生) .

如果我将登录操作放在网址(www.mysite.com/site/login)中,我可以登录 . 但是,如果我在任何控制器动作中进行重定向(例如在更新一些数据之后),我会被抛到主URL而不是我重定向到的路径 . 如果我使用.htaccess重写规则没有区别,所以我想它必须是Yii中的一些配置结合一些特定于apache的配置...

这是我的urlManager配置:

'homeUrl'=>array('/site/index'),
'components'=>array(
    'user'=>array(
        'loginUrl'=>array('site/login'),    
        // disable cookie-based authentication
        'allowAutoLogin'=>false,
        'class'=>'MyWebUser'    
    ),

    'urlManager'=>array(
        'urlFormat'=>'path',
        'showScriptName'=>false,
        'rules'=>array(
            '' => 'site/index',                  
            '<controller:\w+>/<id:\d+>'=>'<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
            '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
            '<module:\w+>/<controller:\w+>/<action:\w+>'=>'<module>/<controller>/<action>',
        ),
    ),
)

这是我的.htaccess文件:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
# RewriteRule . index.php
RewriteRule ^(.*)\?*$ index.php?$1 [L,QSA]

我的网站/登录和网站/索引操作导致无限循环(它们在我的本地计算机上按预期工作):

public function actionIndex()
{
    if(Yii::app()->user->isGuest) {
        $this->redirect('/site/login');
    } else {
        $this->render('index');
    }
}

public function actionLogin()
{
    // redirect to index page if user is already logged in
    if(!Yii::app()->user->isGuest) {
        $this->redirect(Yii::app()->homeUrl);
    }

    $model = new LoginForm;

    // if it is ajax validation request
    if(isset($_POST['ajax']) && $_POST['ajax']==='login-form') {
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }

    // collect user input data
    if(isset($_POST['LoginForm'])) {
        $model->attributes=$_POST['LoginForm'];
        // validate user input and redirect to the previous page if valid
        if($model->validate() && $model->login()) {
            $this->redirect(Yii::app()->user->returnUrl);
        }
    }
    // display the login form
    $this->render('login',array('model'=>$model));
}

直接调用路由有效,重定向不行 .

Edit: 当我从 'urlFormat'=>'path' 切换到 'urlFormat'=>'get' 时,路由工作正常 .

2 回答

  • 1

    而不是 $this->redirect('/site/login'); ,尝试使用 $this->redirect(array('site/login'));

  • 1

    我有类似的问题 . 在我的情况下,这是由于页面中的错误 . 它是在开发工作,但不是因为不同的配置的PHP . 我得到了无限循环,因为在 生产环境 中我从index.php中删除了调试行,所以在出错时,它被重定向到home,导致另一个错误,从而重定向到home,导致错误等等 .

    我通过将调试行投入 生产环境 ,修复错误并删除 生产环境 中的调试行来解决问题 . 当然,在我的情况下它是预先 生产环境 的,如果你在实际 生产环境 中,你可能想要在暂存环境中进行所有这些测试,而不是直接在 生产环境 中 .

相关问题