<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Official FireBird Blog &#187; Symfony</title>
	<atom:link href="http://firebird84vn.wordpress.com/category/symfony/feed/" rel="self" type="application/rss+xml" />
	<link>http://firebird84vn.wordpress.com</link>
	<description>Now and Forever</description>
	<lastBuildDate>Tue, 10 Jul 2007 05:59:51 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='firebird84vn.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/9806f5ba049eb47707b5b13d85d5903d?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Official FireBird Blog &#187; Symfony</title>
		<link>http://firebird84vn.wordpress.com</link>
	</image>
			<item>
		<title>Skipping to Another Action</title>
		<link>http://firebird84vn.wordpress.com/2007/06/30/skipping-to-another-action/</link>
		<comments>http://firebird84vn.wordpress.com/2007/06/30/skipping-to-another-action/#comments</comments>
		<pubDate>Sat, 30 Jun 2007 06:14:37 +0000</pubDate>
		<dc:creator>firebird84vn</dc:creator>
				<category><![CDATA[Symfony]]></category>

		<guid isPermaLink="false">http://firebird84vn.wordpress.com/2007/06/30/skipping-to-another-action/</guid>
		<description><![CDATA[In some cases, the action execution ends by requesting a new action execution. For instance, an action handling a form submission in a POST request usually redirects to another action after updating the database. Another example is an action alias: the index action is often a way to display a list, and actually forwards to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=firebird84vn.wordpress.com&blog=1069990&post=15&subd=firebird84vn&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>In some cases, the action execution ends by requesting a new action execution. For instance, an action handling a form submission in a POST request usually redirects to another action after updating the database. Another example is an action alias: the <code>index</code> action is often a way to display a list, and actually forwards to a <code>list</code> action.</p>
<p>The action class provides two methods to execute another action:</p>
<ul>
<li>If the action forwards the call to another action:
<pre><span class="re0">$this</span>-&gt;<span class="me1">forward</span><span class="br0">(</span><span class="st0">'otherModule'</span>, <span class="st0">'index'</span><span class="br0">)</span>;</pre>
</li>
<li>If the action results in a web redirection:
<pre><span class="re0">$this</span>-&gt;<span class="me1">redirect</span><span class="br0">(</span><span class="st0">'otherModule/index'</span><span class="br0">)</span>; <span class="re0">$this</span>-&gt;<span class="me1">redirect</span><span class="br0">(</span><span class="st0">'http://www.google.com/'</span><span class="br0">)</span>;</pre>
</li>
</ul>
<blockquote><p>The code located after a forward or a redirect in an action is never executed. You can consider that these calls are equivalent to a <code>return</code> statement. They throw an <code>sfStopException</code> to stop the execution of the action; this exception is later caught by symfony and simply ignored.</p></blockquote>
<p>The choice between a redirect or a forward is sometimes tricky. To choose the best solution, keep in mind that a forward is internal to the application and transparent to the user. As far as the user is concerned, the displayed URL is the same as the one requested. In contrast, a redirect is a message to the user&#8217;s browser, involving a new request from it and a change in the final resulting URL.</p>
<p>If the action is called from a submitted form with <code>method="post"</code>, you should always do a redirect. The main advantage is that if the user refreshes the resulting page, the form will not be submitted again; in addition, the back button works as expected by displaying the form and not an alert asking the user if he wants to resubmit a POST request.</p>
<p>There is a special kind of forward that is used very commonly. The <code>forward404()</code> method forwards to a &#8220;page not found&#8221; action. This method is often called when a parameter necessary to the action execution is not present in the request (thus detecting a wrongly typed URL). Listing 6-12 shows an example of a <code>show</code> action expecting an <code>id</code> parameter.</p>
<p class="figure">Listing 6-12 &#8211; Use of the <code>forward404()</code> Method</p>
<pre>public <span class="kw2">function</span> executeShow<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>   <span class="re0">$article</span> = ArticlePeer::<span class="me2">retrieveByPK</span><span class="br0">(</span><span class="re0">$this</span>-&gt;<span class="me1">getRequestParameter</span><span class="br0">(</span><span class="st0">'id'</span><span class="br0">)</span><span class="br0">)</span>;   <span class="kw1">if</span> <span class="br0">(</span>!<span class="re0">$article</span><span class="br0">)</span>   <span class="br0">{</span>     <span class="re0">$this</span>-&gt;<span class="me1">forward404</span><span class="br0">(</span><span class="br0">)</span>;   <span class="br0">}</span> <span class="br0">}</span></pre>
<blockquote><p>If you are looking for the error 404 action and template, you will find them in the <code>$sf_symfony_ data_dir/modules/default/</code> directory. You can customize this page by adding a new <code>default</code> module to your application, overriding the one located in the framework, and by defining an <code>error404</code> action and an error404Success template inside. Alternatively, you can set the error_404_module and error_404_ action constants in the <code>settings.yml</code> file to use an existing action.</p></blockquote>
<p>Experience shows that, most of the time, an action makes a redirect or a forward after testing something, such as in Listing 6-12. That&#8217;s why the sfActions class has a few more methods, named <code>forwardIf()</code>, <code>forwardUnless()</code>, <code>forward404If()</code>, <code>forward404Unless()</code>, <code>redirectIf()</code>, and <code>redirectUnless()</code>. These methods simply take an additional parameter representing a condition that triggers the execution if tested true (for the <code>xxxIf()</code> methods) or false (for the <code>xxxUnless()</code> methods), as illustrated in Listing 6-13.</p>
<p class="figure">Listing 6-13 &#8211; Use of the <code>forward404If()</code> Method</p>
<pre><span class="co1">// This action is equivalent to the one shown in Listing 6-12</span> public <span class="kw2">function</span> executeShow<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>   <span class="re0">$article</span> = ArticlePeer::<span class="me2">retrieveByPK</span><span class="br0">(</span><span class="re0">$this</span>-&gt;<span class="me1">getRequestParameter</span><span class="br0">(</span><span class="st0">'id'</span><span class="br0">)</span><span class="br0">)</span>;   <span class="re0">$this</span>-&gt;<span class="me1">forward404If</span><span class="br0">(</span>!<span class="re0">$article</span><span class="br0">)</span>; <span class="br0">}</span>   <span class="co1">// So is this one</span> public <span class="kw2">function</span> executeShow<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>   <span class="re0">$article</span> = ArticlePeer::<span class="me2">retrieveByPK</span><span class="br0">(</span><span class="re0">$this</span>-&gt;<span class="me1">getRequestParameter</span><span class="br0">(</span><span class="st0">'id'</span><span class="br0">)</span><span class="br0">)</span>;   <span class="re0">$this</span>-&gt;<span class="me1">forward404Unless</span><span class="br0">(</span><span class="re0">$article</span><span class="br0">)</span>; <span class="br0">}</span></pre>
<p>Using these methods will not only keep your code short, but it will also make it more readable.</p>
<blockquote><p>When the action calls forward404() or its fellow methods, symfony throws an sfError404Exception that manages the 404 response. This means that if you need to display a 404 message from somewhere where you don&#8217;t want to access the controller, you can just throw a similar exception.</p></blockquote>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/firebird84vn.wordpress.com/15/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/firebird84vn.wordpress.com/15/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/firebird84vn.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/firebird84vn.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/firebird84vn.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/firebird84vn.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/firebird84vn.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/firebird84vn.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/firebird84vn.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/firebird84vn.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/firebird84vn.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/firebird84vn.wordpress.com/15/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=firebird84vn.wordpress.com&blog=1069990&post=15&subd=firebird84vn&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://firebird84vn.wordpress.com/2007/06/30/skipping-to-another-action/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/398741d3a6b8beddeb1f0cf3971b706a?s=96&#38;d=identicon" medium="image">
			<media:title type="html">firebird84vn</media:title>
		</media:content>
	</item>
	</channel>
</rss>