Redirect to a controller


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


For more info

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) {


		} else {




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','id'=>$model->id, 'title'=>$model->title));


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



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…


@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


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


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

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! 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




			// validate user input and redirect to the previous page if valid

			if($model->validate() && $model->login())

			// since my controller is /protected/controllers/AppController.php



Voilà! You are on your way to building your app!


Not working

try this


Muy útil el contenido, gracias!!!