Weird Urlmanager Activity - Maybe Ghosts

I have been developing locally on MAMP with no probelms. Today I added this to my config/main.php to beautify the urls.




'urlManager'=>array(

            'urlFormat'=>'path',

            'rules' => array(

                '' => 'video/player',

            ),

        ),



Keep in mind that everything works fine in my program without this code. After I added the code I go to mysite.com/index.php/site/login and chrome tells me:

"This webpage has a redirect loop

The webpage at mysite.com/index.php/site/login has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer."

Here is my controller code for site/login




    public function actionLogin()

    {

        $this->layout='login';

        $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(Yii::app()->user->returnUrl);

        }

        // display the login form

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

    }



Anybody have any idea what is causing the problem here when I switch on the UrlManager???

Hi Gilberg,

[s]

This is an unusual set of rules, which probably routes all requested urls to a single route of ‘video/player’. When ‘video/palyer’ needs authentication, then it will redirect the request to the login page, but when the user’s browser has requested the login page, then it will be routed to ‘video/player’ … infinite loop.

So, what do you want to do with the rules?

[/s]

[EDIT] Sorry, I have misunderstood the problem. Let me think for a while …

I couldn’t reproduce the issue …

The most suspicious lines are here, but, I don’t know what’s happening. :(

I also tried to reproduce this with no luck. I get:

Error 404

Unable to resolve the request "video/player"

What code do you have in the player action? Also any before/afterAction() declarations?

I found what was causing the problem. I also have this code in my config




'behaviors' => array(

        'onBeginRequest' => array(

            'class' => 'application.components.SubdomainLogin'

        )

    ),



In the SubdomainLogin class I had this redirect




Yii::app()->user->loginRequired();



I commented it out and everything works as expected. I am still not sure why it worked fine before with no UrlManager.

Seems like the login page itself requires login.

Goes to login --> You have to be loggend in before you can see this page --> Goes to login --> You have to be logged in before you can see this page --> …

Isn’t there something in your SubdomainLogin class that can be affected by the url rule?

Here is my SubdomainLogin Class




class SubdomainLogin extends CBehavior

    {


        /* This method will receive the application as an argument. The attachEventHandler() function attaches to the application an event handler, saying that when the onBeginRequest event occurs, this class’s handleBeginRequest() method should be called. */


        public $domain;


        public function __construct()

        {

            $this->domain=explode('.', $_SERVER['HTTP_HOST']);


        }


        public function attach($owner)

        {

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

        }


        public function handleBeginRequest($event)

        {

            // Check to see if the user is logged in or if they are on the login page, if so ignore

            if (Yii::app()->user->isGuest && !$this->onLoginPage())

            {

                if ($this->checkForSubdomain())

                {

                    if ($this->validateSubdomain())

                    {

                        // The subdomain is valid, redirect to the login page

                        //Yii::app()->user->loginRequired();

                    } else {

                        // The subdomain is not valid

                    }

                }

            }

        }


   public function onLoginPage(){

             // Check to see if their is a GET variable 'r'

             if (isset($_GET['r']))

             {

                if (strpos($_GET['r'], 'login')!==false)

                {

                    return true;

                } else {

                    return false;

                }

             }


             return false;

        }


        public function checkForSubdomain()

        {

            if (count($this->domain)!==3)

            {

                return false;

            }

            else if ($this->domain[0]=='www')

            {

                return false;

            }


            return true;

        }

        public function validateSubdomain()

        {


            // Check to make sure there are only three pieces

            if (!(count($this->domain)==3))

            {

                return false;

            } else {

                // Check to see if the domain is valid

                $client=new Client;

                $client=Client::model()->find(array(

                    'select'=>'id',

                    'condition'=>'sub_domain=:sub_domain',

                    'params'=>array(':sub_domain'=>$this->domain[0])

                ));

                // If the record exists then write it to the session

                if ($client===null)

                {

                    // Do something to show the subdomain does not exist

                    return false;

                } else {

                    Yii::app()->session['client_id']=$client->id;

                    return true;

                }

            }

        }

    }



I am going to have to take a step back and see how this is working. Right now, it is working how I want but I am not sure how because I commented out the


Yii::app()->user->loginRequired();

in the SubdomainLogin class. For instance, if someone goes to www.mysite.com it will show the regular public layout. But if they go to subdomain1.mysite.com. It will show the login page for their particular subdomain. That is the action I want and it is working, but I think I need to refactor and straighten some things out.

Ah, this is it. It will always return false.

There’s no $_GET[‘r’] when you use the path format for the url manager.

Thank you Softark! I didn’t think of that. When I stare at the moon tonight I will think of you and the pain you saved me.