0

我被这个困住了...

我有一个 Yii 项目(Yii 1.1.14),它在我的本地机器上运行良好(IIS 7.5 with ISAPI rewrite 3, Php 5.5)。

然而,在测试服务器(Linux、Apache 2.2、Php 5.5)上,除了重定向之外,一切正常。我在主页上得到一个无限重定向循环,因为登录和索引控制器正在相互重定向(这根本不应该发生)。

如果我将登录操作放在 url (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));
}

直接调用路由有效,重定向无效。

编辑:当我从 切换'urlFormat'=>'path'到 时'urlFormat'=>'get',路由工作。

4

2 回答 2

1

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

我已经通过将调试线投入生产、修复错误并在生产中删除调试线来解决。当然,在我的情况下,它是 pre-prod,如果您在实际生产中,您可能希望在暂存环境中进行所有这些测试,而不是直接在生产中。

于 2014-11-30T22:43:20.543 回答
1

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

于 2014-11-28T17:13:09.610 回答