简体   繁体   English

在Yii Framework中查看网站之前进行初始化登录

[英]Init Login Before View Website in Yii Framework

same as title, i want everybody when visit my site, first, must be login (look like facebook or twitter...) (i using yii framework) i try using: 与标题相同,我希望所有人在访问我的网站时都必须先登录(看起来像facebook或twitter ...)(我使用yii框架),我尝试使用:

'components'=>array(
    'user'=>array(
        'loginUrl'=>array('site/login'),
    ),
);

or: 要么:

public function init()
    {
        if($error=Yii::app()->errorHandler->error)
        {
            if(Yii::app()->request->isAjaxRequest)
                echo $error['message'];
            else
            {
                if(Yii::app()->user->isGuest)
            {
                $this->layout='//layouts/login';
                $this->render('error',$error);
            }
            }
        }
        else
        {
            if(Yii::app()->user->isGuest)
            {
                $this->layout='//layouts/login';
                $this->render('login');
            }
        }
    }

but not work, somebody can help me??? 但没有用,有人可以帮我吗???

I see 2 possible ways here: 我在这里看到2种可能的方式:

Preloading the component: 预加载组件:

I'm assuming that the init() -function you shared here is part of your own application-component (perhaps a derived CWebUser , which would make most sense). 我假设您在此处共享的init()函数是您自己的应用程序组件的一部分(也许是派生的CWebUser ,这是最有意义的)。 If so, just add the "user" to the "preload" setting of your config (so before the components starts: 如果是这样,只需将“用户”添加到配置的“预加载”设置中(这样在组件启动之前:

return array(
   ...
   'preload' => array('user'),
   'components' => array(
       ...
);

(normally it should already be there, since the "log" component is also pre-loaded). (通常它应该已经存在,因为“ log”组件也已预加载)。

1 remark though: If this is derived of another CApplicationComponent , don't forget to call the parent init function . 不过请注意一点:如果这是从另一个CApplicationComponent派生的,请不要忘记调用父init函数

Controller beforeAction : 控制器beforeAction

Add your own controller and use that as a base for everything: 添加您自己的控制器,并将其用作一切的基础:

class MyController extends CController
{
    protected boolean beforeAction(CAction $action)
    {
        // Check if user is logged in and do logic if not
        return parent::beforeAction($action);
    }
}

Given your init() -function, the first might be the easiest to implement. 给定您的init()函数,第一个可能最容易实现。

Use the Yii access control. 使用Yii访问控制。 Read all about it here : http://www.yiiframework.com/doc/guide/1.1/en/topics.auth 在这里阅读所有相关信息: http : //www.yiiframework.com/doc/guide/1.1/en/topics.auth

The way you will do this is by adding a special function to your controller : 您将通过向控制器添加特殊功能来做到这一点:

class MyController extends CController
{

public function accessRules()
{
    return array(
        array('allow',  // allow all users to perform 'list' and 'show' actions
            'actions'=>array('login','register'),
            'users'=>array('*'),
        ),
        array('allow', // allow authenticated user to perform 'create' and 'update' actions
            'users'=>array('@'),
        ),
        array('deny',  // deny all users
            'users'=>array('*'),
        ),
    );
}
...
}

The name of the function must be accessRules. 该函数的名称必须为accessRules。

It works as follows :- 它的工作原理如下:

  • All users are allowed to access the login and register actions. 允许所有用户访问登录和注册操作。 You must have this otherwise user's will have to log in to go to the login page ! 您必须具有此权限,否则用户将必须登录才能进入登录页面!
  • Users that are authenticated (in other words, logged in successfully), are allowed to perform all other actions 经过身份验证的用户(换句话说,成功登录)被允许执行所有其他操作
  • If somehow somebody falls through the loops, we deny all other actions as a safety net. 如果有人以某种方式陷入困境,我们将所有其他行动视为安全网。

Look here for a good example and explanation on how to implement this : http://www.larryullman.com/2010/01/14/yii-framework-access-control-lists/ 在此处查找有关如何实现此功能的好示例和说明: http : //www.larryullman.com/2010/01/14/yii-framework-access-control-lists/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM