Required Login

Below are code snippets I am using. It works great but I want to allow access to one more page that is located under /site/pages/news.php. I attempted to add it to the array several different ways but I am stillb being forced to login on this page.

***config main.php

‘behaviors’ => array(

'onBeginRequest' => array(

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




class RequireLogin extends CBehavior


public function attach($owner)


	$owner->attachEventHandler('onBeginRequest', array($this, 'handleBeginRequest'));


public function handleBeginRequest($event)



	$app = Yii::app();

	$user = $app->user;		 

	$request = trim($app->urlManager->parseUrl($app->request), '/');

	$login = trim($user->loginUrl[0], '/');

	// Restrict guests to public pages.

	$allowed = array($login,'site/index','site/contact');

	if ($user->isGuest && !in_array($request, $allowed))


	 // Prevent logged in users from viewing the login page.

	 $request = substr($request, 0, strlen($login));

	 if (!$user->isGuest && $request == $login)


		 $url = $app->createUrl($app->homeUrl[0]);





Dear Friend

To simulate your scenario, this is the working code in my localhost.


class ApplicationBehavior extends CBehavior


    public function events()


        return array(





    public function restrict()

    {   $owner=$this->owner;




            array("site/page",array('view'=>'news')), //included "index.php?r=site/page&view=news"



        foreach($routes as $route)


        if($owner->user->isGuest && !in_array($owner->request->url,$allowed))


        if(!$owner->user->isGuest && $owner->request->url==$owner->createUrl('site/login'))

            throw new CHttpException(" :You have already logged in");//Throwing error is not a good option though.



Attach this behavior in main.php