This Webpage Has A Redirect Loop When Trying To Login Admin Module

I was created admin module with login.

When entering,


site.com/admin/default/login

It shows This webpage has redirect loop.

Before Login was implemented it shows index page without any error. After login, I got this kind of error.

DefaultController.php




<?php


class DefaultController extends Controller

{

        public function accessRules()

	{

		return array(

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

				'actions'=>array('login'),

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

			),

                        array('deny',

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

                        ),

		);

	}

	public function actionIndex()

	{

		$this->render('index');

	}

        /**

	 * Displays the login page

	 */

	public function actionLogin()

	{

            $model=new LoginForm;

            // if it is ajax validation request

            if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')

            {

                    echo CActiveForm::validate($model);

                    Yii::app()->end();

            }


            // collect user input data

            if(isset($_POST['LoginForm']))

            {


                    $model->attributes=$_POST['LoginForm'];


                    // validate user input and redirect to the previous page if valid

                    if($model->validate() && $model->login())

                    {

                            $this->redirect(array('index'));

                    }


            }

            // display the login form

            $this->render('login',array('form'=>$model));

	}

        /**

	 * Logs out the current user and redirect to homepage.

	 */

	public function actionLogout()

	{

		Yii::app()->user->logout(false);

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

	}

        /**

	 * This is the action to handle external exceptions.

	 */

	public function actionError()

	{

		if($error=Yii::app()->errorHandler->error)

		{

			if(Yii::app()->request->isAjaxRequest)

				echo $error['message'];

			else

				$this->render('error', $error);

		}

	}

        

}



AdminModule.php




<?php


class AdminModule extends CWebModule

{

	public function init()

	{

		$this->layoutPath = Yii::getPathOfAlias('admin.views.layouts');

		$this->layout = 'main';

		// this method is called when the module is being created

		// you may place code here to customize the module or the application

		parent::init();

		

		Yii::app()->setComponents(array(

			'errorHandler'=>array(

			'errorAction'=>'admin/default/error',

		),

		));


		$this->setImport(array(

			'admin.models.*',

			'admin.components.*',

		));

	 

		$this->setComponents(array(

		'errorHandler' => array(

		'errorAction' => 'admin/default/error'),

                    'user' => array(

                    'class' => 'CWebUser',

                    'loginUrl' => Yii::app()->createUrl('admin/default/login'),

                    )

		));

		

		Yii::app()->user->setStateKeyPrefix('_admin');

	}

	public function beforeControllerAction($controller, $action)

	{

		if (parent::beforeControllerAction($controller, $action)) {

		// this method is called before any module controller action is performed

		// you may place customized code here

		$route = $controller->id . '/' . $action->id;

		// echo $route;

		      $publicPages = array(

			'default/login',

			'default/error',

			'default/captcha',

		      );

                    if (Yii::app()->user->isGuest && !in_array($route, $publicPages))

                    {            

                      Yii::app()->getModule('admin')->user->loginRequired();                

                    }

                    else

                      return true;

                    }

                else

                return false;

	}

}




I don’t know where it goes wrong, any help can be appreciable.

Solution !!!

Above Code everything is correct ;D

The problem is in my UrlManager :mellow:

I removed all rules from my UrlManger ( for confirming it works or not )it works ;D

Then i added this code


'admin/login'=>'admin/default/login',

to my url rules.

Finally I got it works myself.

If it helps for you press +1 ;D

Thanks for posting your solution, it may help others, and that’s what we are here for B)