Yii Framework Forum: Newbie Here - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Newbie Here how to point to login page automatically? Rate Topic: -----

#1 User is offline   sam3739 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 15-February 13

Posted 03 April 2013 - 04:01 AM

I've recently started using Yii. My question is how do you automatically point your website to the default login page created by the yiic command?
0

#2 User is offline   hiral darji 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 31-May 12
  • Location:Ahmedabad

Posted 03 April 2013 - 06:53 AM

View Postsam3739, on 03 April 2013 - 04:01 AM, said:

I've recently started using Yii. My question is how do you automatically point your website to the default login page created by the yiic command?


You have to redirect from your SiteController/actionIndex
e.g
public function actionIndex()
	{
		$this->redirect(array('/shop/products/index'));
	}

0

#3 User is offline   JFReyes 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 436
  • Joined: 28-October 09
  • Location:Puerto Rico

Posted 03 April 2013 - 08:57 AM

View Posthiral darji, on 03 April 2013 - 06:53 AM, said:

You have to redirect from your SiteController/actionIndex
e.g
public function actionIndex()
	{
		$this->redirect(array('/shop/products/index'));
	}



No, for the login page created by the yiic command use:
public function actionIndex()
{
    $this->redirect('/site/login');
}

However this will get you into trouble if the user is already logged in because it's an infinite loop. A better way is:
public function actionIndex()
{
    if (Yii::app()->user->isGuest)
        $this->redirect('/site/login');
    else
    {
         // your regular index page code
    }
}

José
0

#4 User is offline   Ride 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 29-December 12

Posted 03 April 2013 - 09:45 AM

If the user should be redirected to the login page due to restriction on the action he/she is trying to access you could just set up AccessRules in your products controller and Yii would handle the redirect automatically.

Here's an example
public function accessRules()
{
   return array(
	array('allow', 
              'actions' => array('index',),
	      'users' => array('@'),
        ),
	array('deny',  // deny all users
	      'users' => array('*'),
	),
   );
}

0

#5 User is offline   JFReyes 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 436
  • Joined: 28-October 09
  • Location:Puerto Rico

Posted 03 April 2013 - 10:31 AM

View PostRide, on 03 April 2013 - 09:45 AM, said:

If the user should be redirected to the login page due to restriction on the action he/she is trying to access you could just set up AccessRules in your products controller and Yii would handle the redirect automatically.

Here's an example
public function accessRules()
{
   return array(
	array('allow', 
              'actions' => array('index',),
	      'users' => array('@'),
        ),
	array('deny',  // deny all users
	      'users' => array('*'),
	),
   );
}



Yes, this is more elegant indeed but I think the OP wanted a "quick and dirty" solution. ;)
José
0

#6 User is offline   sam3739 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 15-February 13

Posted 03 April 2013 - 07:44 PM

thank you very much guys!
0

#7 User is offline   hiral darji 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 31-May 12
  • Location:Ahmedabad

Posted 04 April 2013 - 12:49 AM

View PostJFReyes, on 03 April 2013 - 08:57 AM, said:

No, for the login page created by the yiic command use:
public function actionIndex()
{
    $this->redirect('/site/login');
}

However this will get you into trouble if the user is already logged in because it's an infinite loop. A better way is:
public function actionIndex()
{
    if (Yii::app()->user->isGuest)
        $this->redirect('/site/login');
    else
    {
         // your regular index page code
    }
}



I agree :rolleyes:
0

#8 User is offline   sam3739 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 15-February 13

Posted 15 April 2013 - 08:35 PM

View Posthiral darji, on 04 April 2013 - 12:49 AM, said:

I agree :rolleyes:


Thanks everyone, by the way is it possible to create a web app without using the yiic? I'm trying to re create what I've done with yiic but I'm encountering a page not found error
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users