Redirect to a controller

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:

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.yiiframework.com/doc/api/1.1/CController#redirect-detail

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.

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.yiiframework.com/forum/index.php/topic/10263-how-to-redirect/page__p__50488#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.

Hi NaX,

I tried $this->redirect(array(‘user/index/’))… it is redirecting to user/index…

Thanks

@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 Like

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)

Thanks to Y!! in following post

http://www.yiiframework.com/forum/index.php/topic/5372-change-default-controller-action/page__view__findpost__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()

  {


  }


}

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

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!

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

Not working

try this




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

Muy útil el contenido, gracias!!!