Error: Trying to get property of non-object

Just moved my app to a production server and having a problem. I require the user to login to access any page other than the login page with the following code. It works fine on my localhost (XAMPP), but the production server gives me the error: Trying to get property of non-object (focused on the Yii::app()->controller->id). Am I missing a module or have another problem?

In: main.php


'behaviors' => array(

    	'onBeginRequest' => array(

        	'class' => 'application.components.RequireLogin'

    	)

),

In: RequireLogin


if (Yii::app()->user->isGuest && Yii::app()->controller->id == 'login'

CWebApplication::onBeginRequest() is called before controller is loaded so Yii::app()->controller contains NULL value.

Set error_reporting to E_ALL in php.ini and you will get the same error on your localhost.

Thanks, managed to ‘fix’ the problem by stripping out controller name from the server request. Not as pretty but it works.

I have the same problem friend any one to help?

I use a different method to do this.

All my user-level controllers inherit from a custom base controller. In that controller I have code that checks if user isGuest and redirects to login page.

example:


	public function beforeAction()

	{

        if(Yii::App()->user->isGuest) { 

            $this->redirect(Yii::app()->user->loginUrl); 

                return false; 

        }   elseif (!$this->isOwnerOrAdmin()) {

				throw new CHttpException(403, 'Action is forbidden. Hit the road, Jack.');

			}

			return true;

	}

	 

My isOwnerOrAdmin function just checks if the user has role of admin or ‘owns’ the content.