manual controller loading

How can i load a controller manually? using require_once or something and running one of it’s actions?

The reason i want this is because i need to make a layout that redirects the member to another location with a message. Something like "You are now redirected to XXX…" and after 2 secs the page will be refreshed to the url.

If there is a better way of doing this i would love to know.

Thanks.


<meta http-equiv="refresh" content="2; URL=http://...">

?

How can i load a controller manually?

I like mbi’s approach, using the meta tag. You could also use a bit of javascript to delay loading of a new url.

If you’re just looking for a way to have one view call an action in another controller, you could do something like this:

extend CAction instead of adding a method to your controller. you’ll have to add the new action to the controller via the actions() method. of course, you’ll have to add this action to any controller you’d want to call it from.




public function actions()

{

    return array(

        'myAction'=>'application.controllers.myController.MyAction',

    );

}



Then, to make sure you render the proper view from this CAction class, you gotta be careful how you do the render() call.




//don't do this:

$this->render('view', array());


//specify controller

$this->getController()->render('/controller/view', array());



Meta tag is something i am very familiar with, And not what i meant with my question.

I am trying to make a redirect function that will display a redirect screen with a message. So i will be able to do inside a controller something like:


$this->doRedirect(URL HERE);

When that function is called it will load a certain layout (a different one from the current used of course) and a view, Inside the layout in the head there will be a meta refresh tag that will redirect the page to somewhere else with a delay of two seconds so the member could actually see and read the message displayed before the redirect occurs.

From what i have understood and read on Yii documentation, The only way of doing this is to create a controller with a different layout assigned, And by calling a certain action in that controller it will then render the required view in the assigned layout and will display the redirect screen. of course i will also need to pass in variables like the url where it needs to redirect to.

That’s what i am trying to do, I tried adding an actions function in my base controller but didn’t quite get it to work properly. Any suggestions and help will be much appreciated.

maybe just another option

instead of that, why not use flash messages?

http://www.yiiframework.com/doc/cookbook/21/

How can i possibly use that to achieve what i have listed?

I think I understand what you’re trying to do… So, how about this?

Controller:




class YourController extends CController {

   public function doRedirect($url) {

      $this->layout = 'YOUR_REDIRECTION_LAYOUT';

      $this->render('YOUR_REDIRECTION_VIEW', compact('url'));

      die;

   }


   public function actionTest() {

      if (isset($_GET['redirect'])) {

         $this->doRedirect($this->createUrl('otherController/action'));

      }

      echo "Not redirecting...";

   }

}



Layout:




<html>

<head>

   <title>My redirection layout</title>

</head>

<body>

   <?php echo $content ?>

</body>

</html>



View:




<?php Yii::app()->clientScript->registerMetaTag("2; URL=$url", null, 'refresh') ?>

<h1>You're being redirected to <?php echo $url ?>...</h1>



Although in this case the layout is kind of unneccessary. I’d just set the layout to false and put all the markup in a single view.

Yes Kaerigan, That did it. Thanks a lot.