Require Login

I’m new to YII Framework. My past web development work has been using Python frameworks. I’m trying to figure out how to require login for all pages. I have started a new project with the yiic tool. However, on default all pages are accessible. I want the user to be required to login before accessing pages. Thanks in advance.

In the controller -in accessRules()-, change the ‘user’ key value of respective action to ‘@’.

In Yii: * => everyone, @ => logged users, ? => anonymous, ‘name’ => user’s name.

Happy coding.

I’m fairly new too Yii as well, but I’ll give this a shot.

What I think you want to explore is the accesRules function of your controllers for each model. Here you can set rules regarding which actions such as view,update,delete, etc can be viewed by what types of users.


	public function accessRules()

	{

		return array(

			array('allow',  // allow all users to perform 'index' and 'view' actions

				'actions'=>array('index','view','aclist', 'leadlist','countryRate','excel'),

				'users'=>array('*'),

			),

			array('allow', // allow authenticated user to perform 'create' and 'update' actions

				'actions'=>array('create','update'),

				'users'=>array('@'),

			),

			array('allow', // allow admin user to perform 'admin' and 'delete' actions

				'actions'=>array('admin','delete'),

				'users'=>array('admin'),

			),

			array('deny',  // deny all users

				'users'=>array('*'),

			),

		);

	}

Yes! Thanks! That’s what I was looking for you. Still learning the ins and outs of this framework. Seems pretty cool though!

… and if you don´t want to do this in each and every controller simply move the function accessRules() to

/components/controller.php

Have a look at http://www.yiiframework.com/wiki/121/extending-common-classes-to-allow-better-customization/

Wow! Thanks! Thank you to all that have responded. Rarely have I received this type of prompt, polite, and informative response from other development tools.

Follow this wiki -

http://www.yiiframework.com/wiki/738/forcing-login-for-all-pages-in-yii/

here is what i use. Requre login for all pages …it’s where Rohit Suthar wiki came from… his site has tons of great info. You will probably need to read the comments if you have trouble getting it to work.

doing this method would require to edit all actions in every controller. It would be more practical to do it before any request.