I have a simple question:[color="#0000FF"] What’s the best way to implement the PRG Pattern in Yii?[/color]
I’m building a SAAS, so in my case I try to create the PRG Pattern with this architecture:
1- createActionUrl($actionName, $params = null) //Custom function that append action triggers to $_GET
[indent]returns the target url + $_GET["actionWidget"] + $_GET["actionWidgetID"] + $_GET["prg"]//$params[/indent]
2- actionListener()
[indent]if ( isset($_GET[‘actionWidget’]) ) then call the action on the selected “widget”[/indent]
3- CController::afterAction($action) //This is where I implement the PRG Pattern
public function afterAction($action)
{
$this->postRedirectGet();
parent::afterAction($action);
}
public function postRedirectGet()
{
if( isset($_GET['actionWidget']) )
{
$get = $_GET;
if( isset($_GET['actionWidgetClass']) )
{
unset($get['actionWidgetClass']);
}
if( isset($_GET['actionWidgetID']) )
{
unset($get['actionWidgetID']);
}
if( isset($_GET["prg"]) )
{
unset($get['prg']);
}
unset($get["actionWidget"]);
$param = implode("/", $get); //I use path URL
$this->redirect( Yii::app()->request->requestUri . $param );//Here I try to stitch up my redirect
}
}
My logic:
[list=1]
[*]POST my form to createActionUrl; //Works
[*]Execute the action in my selected widget(write in DB); //Works
[*]CController::afterAction(); // call postRedirectGet() //Works
[*]postRedirectGet() //clean up the $_GET of all "action" and "prg" params //Works
[*]Stitch up a new $cleanUrl;
[*]$this->redirect( $cleanUrl );
[*]Load fresh page.
[/list]
I get:
[indent][color="#FF0000"]
Cannot modify header information - headers already sent by (output started at /var/www/gtbHosting/protected/yii/framework/web/CController.php:794)
/var/www/gtbHosting/protected/yii/framework/web/CHttpRequest.php(715)
703 /**
704 * Redirects the browser to the specified URL.
705 * @param string $url URL to be redirected to. If the URL is a relative one, the base URL of
706 * the application will be inserted at the beginning.
707 * @param boolean $terminate whether to terminate the current application
708 * @param integer $statusCode the HTTP status code. Defaults to 302. See {@link http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html}
709 * for details about HTTP status code.
710 */
711 public function redirect($url,$terminate=true,$statusCode=302)
712 {
713 if(strpos($url,’/’)===0)
714 $url=$this->getHostInfo().$url;
715 header('Location: '.$url, true, $statusCode);
716 if($terminate)
717 Yii::app()->end();
718 }
719
[/color][/indent]
I read that it’s because I echo something out BEFORE the redirect, which in my case I guess it’s because I render the page with my collection of widgets.
In any case, is the CController::afterAction() the right place to implement this?
Why I’m having a [color="#FF0000"]“Cannot modify header information”[/color] error?
I would appreciate any thought on my approach to this problem thank you