Yii Framework Forum: Home page redirect, best practice? - Yii Framework Forum

Jump to content

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

Home page redirect, best practice? Rate Topic: -----

#1 User is offline   jodev 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 75
  • Joined: 10-January 11
  • Location:The Netherlands

Posted 22 October 2011 - 10:40 AM

Quick question. When a user accesses my site, I want the homepage to be one of two pages, depending whether the user is logged in or not. What is the best practice?

Should I use the site index controller and do a redirect to the right controller and action (and if so, which HTTP status code would be most suitable) or is there a way that does not require an HTTP redirect?
0

#2 User is offline   dckurushin 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 392
  • Joined: 16-March 11
  • Location:Israel

Posted 22 October 2011 - 12:27 PM

I think better just redirect... but you can also just call action from another controller... not tried but it is possible I'm sure
0

#3 User is offline   putera 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 17-October 11

Posted 22 October 2011 - 12:41 PM

View Postjodev, on 22 October 2011 - 10:40 AM, said:

Quick question. When a user accesses my site, I want the homepage to be one of two pages, depending whether the user is logged in or not. What is the best practice?

Should I use the site index controller and do a redirect to the right controller and action (and if so, which HTTP status code would be most suitable) or is there a way that does not require an HTTP redirect?


I'd use conditional, not a redirection.

In the controller you can choose the correct templates,
  public function actionIndex() {
    if (!Yii::app()->user->isGuest) {
      //
      $this->render('index_user');
    }
    else {
      //
      $this->render('index_guest');
    }
  }


In the template,
<?php if (!Yii::app()->user->isGuest): ?>
...
<?php else: ?>
...
<?php endif; ?>


To increase page speed, minimize redirection, see.
1

#4 User is offline   macinville 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 268
  • Joined: 25-October 10
  • Location:Marikina City, Philippines

Posted 22 October 2011 - 01:50 PM

You can use the controller's accessRules method, and give the 'homepage' access only to those who are authenticated, like

     public function accessRules() {
	return array(
            // redirect to login page if not logged in
	    array('allow', 
		'actions' => array('login'),
		'users' => array('*'),
	    ),
            // allow only if the user has been authenticated
	    array('allow', 
		'actions' => array( 'index'),
		'users' => array('@'),
	    ),
            // deny the rest of the requests
	    array('deny', // deny all users
		'users' => array('*'),
	    ),
	);
    }

Tell your tail.
Don't be shy to upvote a post if it was able to help you
0

#5 User is offline   jodev 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 75
  • Joined: 10-January 11
  • Location:The Netherlands

Posted 23 October 2011 - 04:06 AM

Maybe I should clarify that I need to 'redirect' the homepage to of of two controllers. In fact, these controllers each belong to a different module. When the user is not logged in, I want the homepage to show user/login/login. However, when the user is already logged in, it should call pictures/overview/index.

While I really appreciate the suggestions above, I'm not sure they work in this case.
0

#6 User is offline   macinville 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 268
  • Joined: 25-October 10
  • Location:Marikina City, Philippines

Posted 25 October 2011 - 05:47 AM

I suggest you do these:

  • in your config.php, set 'defaultController' to 'pictures' (it should look for PicturesModule according to this),and 'loginUrl' to '//user/login/login'
  • in your PictureModule, set 'overview' as your 'defaultController'
  • in your OverviewController, set 'defaultAction' to 'index' (actually, since your action's name is index, you can skip that), and configure accessRules method so that 'index' can only be accessed by authenticated users


Say, a user visits your site for the first time, Yii will try to load '//pictures/overview/index' to him because that is your homepage. But Yii detected that the user is not yet authenticated, so the user will be routed to the login page, which is '//user/login/login'. After logging in, he will be automatically routed to your homepage. If the user is already logged in, he will be able to access your homepage without any rerouting.
Tell your tail.
Don't be shy to upvote a post if it was able to help you
1

#7 User is offline   jodev 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 75
  • Joined: 10-January 11
  • Location:The Netherlands

Posted 25 October 2011 - 04:56 PM

Thanks macinville. Not sure why I didn't think of that myself, but it's easy to implement and a nice clean solution to the problem. Much appreciated. :)
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