How do I call a method before every controller action

Is there any kind of controller constructor, or some kind of before filter or something?

Please explain better what you want with it.

What is the purpose?

There may be a better solution.

beforeAction?

http://www.yiiframework.com/doc/api/1.1/CController#beforeAction-detail

Yes beforeAction looks good.

How would I use it though?

What I am trying to accomplish is to deny the user access to certain controllers or methods based on whether the user is ->isGuest …

Check the documentation for beforeAction() - http://www.yiiframework.com/doc/api/1.1/CController#beforeAction-detail

If it returns false the action will not be executed…

For this there is already access rules:





	public $town;

	/**

	 * @return array action filters

	 */

	public function filters()

	{

		return array(

			'accessControl', // perform access control for CRUD operations

		);

	}


	

	/**

	 * Specifies the access control rules.

	 * This method is used by the 'accessControl' filter.

	 * @return array access control rules

	 */

	public function accessRules()

	{

		return array(

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

				'actions'=>array('index','contact'),

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

			),

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

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

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

			),

			array('deny',  // deny all users

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

			),

		);

	}

	




You don’t have to implement anything

Ahh well, my access control need to work of the "role" column in the user database table. The role column is an integer.

Is this possible using the way you have described.




public function accessRules()

        {

                return array(

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

                                'actions'=>array('index','contact'),

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

                        ),

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

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

                                'expression'=>'here goes the php expression to check for the user role',

                        ),

                        array('deny',  // deny all users

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

                        ),

                );

        }



Thanks.

And thanks for the other responses as well people.