Problem: how to show the mainpage

I have a page with a CLOSE button. The CLOSE button should result in updating the database and returning to the mainpage.

Normally the mainpage is SiteController.actionIndex().

How do I call the default controller with default action from within my controller ?

This is what I’ve come up so far. This does not work, it throws a CException:

YiiBase::include(SiteController.php) [<a href=‘function.YiiBase-include’>function.YiiBase-include</a>]: failed to open stream: No such file or directory

class MyController extends CController

{

public function actionClose()


{


    //... update the database





    &#036;site = new SiteController();


    &#036;site-&gt;actionIndex();


}

}

Thanks in advance,

Mirco

Try a redirect

http://www.yiiframework.com/doc/api/CController#redirect-detail

nz

in action, you can do like this:


$this->redirect($this->createUrl('/site'));

The redirection works, but it is not the solution I’m looking for.

The redirection responds with a http 302 to the browser, indicating what URL should be loaded.

The browser parses the http 302 response and issues a new request with the URL indicated in the http-header LOCATION.

This does load the mainpage.

BUT to me, this is one http request too much. Why should the browser do the redirection ?

The server should just render the mainpage. And that is what I want, call SiteController.actionIndex(). The problem is I’m not very familiair with PHP/YII. My solution with instantiating SiteController results in the lazyloader complaining it can’t find SiteController.php.

Maybe I should refrase my question into:

How do I instantiate SiteController from within MyController ?

If I have an instance, I can call function actionIndex() and my problem is solved.

Regards,

Mirco

Ok, I found the solution:




Yii::app()->runController('site/index');



Even though thanks for thinking with me !

Regards,

Mirco

Just a little more sophisticated:




Yii::app()->runController(Yii::app()->defaultController);



(No I’m not a perfectionist ! :)

Regards,

Mirco

You should not be afraid of redirects. They are a standard way of doing a clean page refresh. With HTTP 1.1’s KeepAlive connections there isn’t even a new connection created. And the additional roundrip time shouldn’t even be noticable as no content needs to be transferred on the first response.

Moreover you’ll very soon get problems with POST requests and users clicking the reload button. (“Should the POST data be sent again?”).