Yii Framework Forum: Redirect to a controller - Yii Framework Forum

Jump to content

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

Redirect to a controller How to Redirect from a controller to an another controller? Rate Topic: -----

#1 User is offline   B L Praveen 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 13-March 12

Posted 14 March 2012 - 09:47 AM

Hi,

Can we use more than one controller to handle different tabels. Like User controller to handle all the User create and updates and site controller before logon...?

What I am trying to do is that .. there is two controller UserController and SiteContoller .. when user is not logged on ... the SiteController would redirect to site/index.html .. When the user is logged on.. the SiteController would redirect all request to page site/index.html to user/index.html..


How we have to accomplish this .. is it by changing the urlpath or url manager in application or from sitecontroller actionindex...?



Thanks and Regards
B L :mellow:
0

#2 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 756
  • Joined: 02-February 09

Posted 14 March 2012 - 03:05 PM

This is not the best description of a problem I've seen ;) but to redirect you should use
$this->redirect()
For more info http://www.yiiframew...redirect-detail
0

#3 User is offline   Onman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 175
  • Joined: 26-December 09
  • Location:The Netherlands

Posted 14 March 2012 - 05:18 PM

In your site controller does not implement any filters you can use the controller's beforeAction method to check for a logged in user and then redirect to the user controller.

If you use any filters which prevent the above, you dan use your application class (derived from CWebApplication) method beforeControllerAction to redirect the request.
0

#4 User is offline   B L Praveen 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 13-March 12

Posted 15 March 2012 - 01:25 AM

View PostOnman, on 14 March 2012 - 05:18 PM, said:

In your site controller does not implement any filters you can use the controller's beforeAction method to check for a logged in user and then redirect to the user controller.

If you use any filters which prevent the above, you dan use your application class (derived from CWebApplication) method beforeControllerAction to redirect the request.


Hi onman, What I was expecting is check if the user is guest, if true site/index.php else user/index.php
public function actionIndex()
	{	 
		if(Yii::app()->user->isGuest) {
			$this->render('index');
		} else {
                     $this->redirect(<contoroller>/<action>)
		}
	}


I am not sure what the redirect url would be.. I tried $this->redirect('user') it is redirecting to site/user.. instead i want to redirect to user controller.
0

#5 User is offline   NaX 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 51
  • Joined: 13-January 12
  • Location:Johannesburg

Posted 15 March 2012 - 02:15 AM

I found the following examples on another forum post that could help.

$this->redirect(array('site/author'));

OR
$this->redirect(array('site/author','id'=>$model->id, 'title'=>$model->title));


REF: http://www.yiiframew...0488#entry50488

I also remember seeing something to do with starting with '/' when changing controllers.

EG:
Yii::app()-request->redirect('/path/to/url');


I am very new to Yii, learning as I go, I could be wrong. Hope it helps.
1

#6 User is offline   B L Praveen 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 13-March 12

Posted 15 March 2012 - 02:31 AM

Hi NaX,
I tried $this->redirect(array('user/index/'))... it is redirecting to user/index..
Thanks
0

#7 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 756
  • Joined: 02-February 09

Posted 15 March 2012 - 05:12 AM

@B L Praveen

In Yii there is a default controller id. When you do redirect you can either specify controller/action or just action. When specify only action such as
$this->redirect(array('user'))
Yii will redirect you to the default controller/user. By default the default controller is site hence with the above redirection you will be taken to site/user. In order to redirect to a specific controller you have to explicitly declare it in the redirect method such as
$this->redirect(array('user/index'))
and this will take you to controller User / action Index
1

#8 User is offline   B L Praveen 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 13-March 12

Posted 15 March 2012 - 09:09 AM

hi bettor, thanks for your valuable suggestion.. it is very helpfull..
But I got one more doubt from your we point.. Can I know where this default controller id is set..and whether can we change the default value..
0

#9 User is offline   kokomo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 289
  • Joined: 23-July 10

Posted 15 March 2012 - 09:39 AM

View PostB L Praveen, on 15 March 2012 - 09:09 AM, said:

hi bettor, thanks for your valuable suggestion.. it is very helpfull..
But I got one more doubt from your we point.. Can I know where this default controller id is set..and whether can we change the default value..

There is the CController.defaultAction property which you can set in your controller(s) Posted Image
1

#10 User is offline   NaX 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 51
  • Joined: 13-January 12
  • Location:Johannesburg

Posted 15 March 2012 - 10:53 AM

Thanks to Y!! in following post
http://www.yiiframew...dpost__p__27614

In main.php config you can set your default controller

'defaultController' => 'TestController',


To change the default action of a controller as per kokomo

class TestController extends CController
{

  public $defaultAction = 'test';

  public function actionTest()
  {

  }

}

2

#11 User is offline   B L Praveen 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 13-March 12

Posted 20 March 2012 - 01:05 AM

Thanks NaX!..it is clear to me now..
0

#12 User is offline   dhimes 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 07-August 12

Posted 11 September 2012 - 09:32 AM

View Postbettor, on 14 March 2012 - 03:05 PM, said:

This is not the best description of a problem I've seen ;) but to redirect you should use
$this->redirect()
For more info http://www.yiiframew...redirect-detail


On the contrary: to a non-expert, this is an outstanding description of the problem. It's simple, really: people come to the web page. They log in. After they log in, they go to a different page (with different DOM, different javascripts, etc.).

To do this, create a new controller in gii for the page your user will be directed to after s/he logs in. I'll call this controller 'app'. You'll now have a file /protected/models/AppController.php

In this file, you will have a default public function (method) called actionIndex. The purpose of this default method is to call (render) the /protected/views/app/index.php file. That is the file your users will see once they log in. That is the file you will want to modify. Go back to SiteController.php and change the argument of redirect() in the actionLogin() method

	if(isset($_POST['LoginForm']))
		{
			$model->attributes=$_POST['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login())
			// since my controller is /protected/controllers/AppController.php
				$this->redirect(array('app/index'));
		}


VoilĂ ! You are on your way to building your app!
0

#13 User is offline   HARIKRISHNAN.V 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 06-October 13

Posted 08 October 2013 - 02:15 PM

Yii::app()-request->redirect('/path/to/url')

Not working
0

#14 User is offline   Ankit Modi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 908
  • Joined: 19-February 13
  • Location:India

Posted 08 October 2013 - 11:02 PM

View PostHARIKRISHNAN.V, on 08 October 2013 - 02:15 PM, said:

Yii::app()-request->redirect('/path/to/url')

Not working



try this
Yii::app()->getController()->redirect(array('/path/to/url'));

Thanks,
Ankit Modi
Skype : amodi06
1

#15 User is offline   XerverAyala 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 28-September 17
  • Location:La Libertad, El Salvador, C.A.

Posted 14 November 2017 - 12:23 PM

View PostB L Praveen, on 15 March 2012 - 01:25 AM, said:

Hi onman, What I was expecting is check if the user is guest, if true site/index.php else user/index.php
public function actionIndex()
	{	 
		if(Yii::app()->user->isGuest) {
			$this->render('index');
		} else {
                     $this->redirect(<contoroller>/<action>)
		}
	}


I am not sure what the redirect url would be.. I tried $this->redirect('user') it is redirecting to site/user.. instead i want to redirect to user controller.


Muy Ăștil el contenido, gracias!!!
0

Share this topic:


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

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