如果用户未登录,则重定向到登录页面?

时间:2014-07-14 09:51:09

标签: php redirect yii

我在Controller类中使用beforeAction方法重定向以进行重定向。当我使用$ this->重定向方法重定向到登录页面时,它无效。

该页面未正确重定向`

public function beforeAction($action)
{
    if (Yii::app()->user->isGuest)
    {


        $this->redirect(Yii::app()->createUrl('user/login/login')); 

        return true;
    }
    else
    { 
        return true;
    }

    //something code right here if user valided
    return true;
}

2 个答案:

答案 0 :(得分:0)

试试这个

Yii::app()->user->returnUrl = Yii::app()->request->url;
$this->redirect($this->createUrl('/site/login'));

答案 1 :(得分:0)

这是main.php

<?php

//取消注释以下内容以定义路径别名 // Yii :: setPathOfAlias(&#39; local&#39;,&#39; path / to / local-folder&#39;);

//这是主要的Web应用程序配置。任何可写的 //可以在此处配置CWebApplication属性。 返回数组(     &#39;基本路径&#39; = GT;目录名(文件)DIRECTORY_SEPARATOR&#39;。。。。&#39 ;,     &#39;名称&#39; = GT;&#39; EMS&#39 ;,     //&#39; defaultController&#39; =&GT; &#39;用户/注册/登陆&#39;,

// preloading 'log' component
'preload'=>array('log'),

// autoloading model and component classes
'import'=>array(
    'application.models.*',
    'application.components.*',


    'application.modules.user.models.*',
    'application.modules.user.components.*',
    'application.modules.user.*',
),

'modules'=>array(
    // uncomment the following to enable the Gii tool

    'gii'=>array(
        'class'=>'system.gii.GiiModule',
        'password'=>'admin',
        // If removed, Gii defaults to localhost only. Edit carefully to taste.
        'ipFilters'=>array('127.0.0.1','::1'),
    ),
    'user',


),


//default theme

// application components
'components'=>array(
    'user'=>array(
        // enable cookie-based authentication
         'class' => 'WebUser',
         'allowAutoLogin'=>true,
         //'loginUrl'=>array('user/login'),
    ),
    'authManager' => array(
        'class' => 'CDbAuthManager',
        'connectionID' => 'db'
    ),



    // uncomment the following to enable URLs in path-format

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

    /*'db'=>array(
        'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
    ),*/
    // uncomment the following to use a MySQL database

    'db'=>array(
        'connectionString' => 'mysql:host=localhost;dbname=yiiems',
        'emulatePrepare' => true,
        'username' => 'root',
        'password' => '',
        'charset' => 'utf8',
    ),

    'errorHandler'=>array(
        // use 'site/error' action to display errors
        'errorAction'=>'site/error',
    ),
    'log'=>array(
        'class'=>'CLogRouter',
        'routes'=>array(
            array(
                'class'=>'CFileLogRoute',
                'levels'=>'error, warning',
            ),
            // uncomment the following to show log messages on web pages
            /*
            array(
                'class'=>'CWebLogRoute',
            ),
            */
        ),
    ),
),

// application-level parameters that can be accessed
// using Yii::app()->params['paramName']
'params'=>array(
    // this is used in contact page
    'adminEmail'=>'atinekd@ebizneeds.com',
),

'behaviors' => array(
        'onBeginRequest' => array(
                'class' => 'application.components.RequireLogin'
        )
),

);