我已将我的Yii应用程序上传到运行PHP5和Apache2.x的Ubuntu 12.04服务器。 .htaccess文件包含:
RewriteEngine on
RewriteBase /
/ protected / controllers中的UserController.php包含:
public function actionLogin()
{
$model= new Users();
// if it is ajax validation request
if(isset($_POST['ajax']))
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
// print_r($_POST['Users']);
// collect user input data
if(isset($_POST['Users']))
{
$model->attributes=$_POST['Users'];
// validate user input and redirect to the previous page if valid
if($model->login())
$this->redirect(Yii::app()->user->returnUrl);
}
// display the login form
$this->render('login',array('model'=>$model));
}
当我登录该网站时,收到HTTP404消息。 apache2 error.log状态
file does not exist /var/www/users
如何纠正这404错误?
更新:
看来returnURL没有被重视。如何设置returnUrl以便将用户重定向回上一页?
UPDATE2: 这是config / main.php的一个片段:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
这是主.htacess文件的内容:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin/(.*)\?*$ admin.php/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
php_value upload_max_filesize 20M
php_value post_max_size 21M
更新: 看起来根本原因可能与用于进行身份验证的UserIdentity类有关。以下是用户模型的片段:
public function login()
{
if($this->_identity===null)
{
$this->_identity=new UserIdentity($this->username,md5($this->password));
$this->_identity->authenticate();
}
if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
{
$duration=$this->rememberMe ? 3600*24*30 : 60*20; // 30 days
Yii::app()->user->login($this->_identity,$duration);
return true;
}
else{
$this->addError('password','Incorrect username or password.');
return false;
}
}
建议使用UserIdentity的CdbHttpSession insead来完成登录吗?
答案 0 :(得分:1)
我认为您的.htaccess和Yii主配置存在问题。
http://www.yiiframework.com/doc/guide/1.1/en/topics.url
我想如果你已经正确设置你的config / main.php(你可以用这个更新问题,特别是urlManager部分),你.htaccess需要
RewriteEngine on # 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
答案 1 :(得分:0)
尝试更改
$this->redirect(Yii::app()->user->returnUrl);
到
$this->redirect(Yii::app()->user->getReturnUrl(Yii::app()->homeUrl));
HTH
答案 2 :(得分:0)
您可以使用以下命令显式设置returnUrl:
Yii::app()->user->setReturnUrl('controller/action');
你把它放在哪里,我可能会在你验证密码等时在components/userIdentity.php
课程中设置它,或者你可以在你的$model->validate()
方法中设置它。 / p>