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…?
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.
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
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'));
}