<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: CakePHP, beforeFilter, and the Error Error</title>
	<atom:link href="http://www.bradezone.com/2009/05/21/cakephp-beforefilter-and-the-error-error/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bradezone.com/2009/05/21/cakephp-beforefilter-and-the-error-error/</link>
	<description>Reception. Observation. Perception. Emotion.</description>
	<lastBuildDate>Wed, 21 Dec 2011 06:12:18 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Matt Davis</title>
		<link>http://www.bradezone.com/2009/05/21/cakephp-beforefilter-and-the-error-error/#comment-708</link>
		<dc:creator>Matt Davis</dc:creator>
		<pubDate>Fri, 02 Dec 2011 19:20:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.bradezone.com/?p=549#comment-708</guid>
		<description>Thank you! I&#039;ve been banging my head against the wall for several weeks now, trying to figure out how to supply a useful 404 page, while still showing my custom site settings from my beforeFilter method. This is such an easy fix.</description>
		<content:encoded><![CDATA[<p>Thank you! I&#8217;ve been banging my head against the wall for several weeks now, trying to figure out how to supply a useful 404 page, while still showing my custom site settings from my beforeFilter method. This is such an easy fix.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shane</title>
		<link>http://www.bradezone.com/2009/05/21/cakephp-beforefilter-and-the-error-error/#comment-254</link>
		<dc:creator>Shane</dc:creator>
		<pubDate>Fri, 17 Jun 2011 21:16:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.bradezone.com/?p=549#comment-254</guid>
		<description>This should look a lil cleaner: 


    /**
     * Overload this protected method because it&#039;s called by all the
     * built-in error methods (error404, missingAction, etc) and
     * we need to fix a sneaky little bug caused by the fact that the 
     * CakeErrorController -- while a subclass of AppController and designed
     * to function like any other page controller -- is not instantiated by the 
     * normal Dispatch process and instead has this parallel bootstrap
     * in the ErrorHandler::__construct class. Somehow this was bunged a 
     * little and some of the normal dispatch steps aren&#039;t executed.
     * At a minimum, I know it&#039;s not calling AppController::beforeFilter(). 
     * In our lil app here we use that HEAVILY so this became a big problem for us.
     *
     * @see http://www.bradezone.com/2009/05/21/cakephp-beforefilter-and-the-error-error/
     * @see ErrorHandler::_outputMessage()
     */
    function _outputMessage($template)
    {
        static $virgin = true;

        if ($virgin) {

            $virgin = false;
            $this-&gt;controller-&gt;beforeFilter();
        }
        parent::_outputMessage($template);

    }
</description>
		<content:encoded><![CDATA[<p>This should look a lil cleaner: </p>
<p>    /**<br />
     * Overload this protected method because it&#8217;s called by all the<br />
     * built-in error methods (error404, missingAction, etc) and<br />
     * we need to fix a sneaky little bug caused by the fact that the<br />
     * CakeErrorController &#8212; while a subclass of AppController and designed<br />
     * to function like any other page controller &#8212; is not instantiated by the<br />
     * normal Dispatch process and instead has this parallel bootstrap<br />
     * in the ErrorHandler::__construct class. Somehow this was bunged a<br />
     * little and some of the normal dispatch steps aren&#8217;t executed.<br />
     * At a minimum, I know it&#8217;s not calling AppController::beforeFilter().<br />
     * In our lil app here we use that HEAVILY so this became a big problem for us.<br />
     *<br />
     * @see <a href="http://www.bradezone.com/2009/05/21/cakephp-beforefilter-and-the-error-error/" rel="nofollow">http://www.bradezone.com/2009/05/21/cakephp-beforefilter-and-the-error-error/</a><br />
     * @see ErrorHandler::_outputMessage()<br />
     */<br />
    function _outputMessage($template)<br />
    {<br />
        static $virgin = true;</p>
<p>        if ($virgin) {</p>
<p>            $virgin = false;<br />
            $this-&gt;controller-&gt;beforeFilter();<br />
        }<br />
        parent::_outputMessage($template);</p>
<p>    }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shane</title>
		<link>http://www.bradezone.com/2009/05/21/cakephp-beforefilter-and-the-error-error/#comment-253</link>
		<dc:creator>Shane</dc:creator>
		<pubDate>Fri, 17 Jun 2011 21:12:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.bradezone.com/?p=549#comment-253</guid>
		<description>Also, I had the $virgin check in there because I wasn&#039;t sure if there would be a situation where more than one error is called on that same error object. I&#039;m still not sure if that&#039;s the case, so rather than risk calling beforeFilter et al more than once, I&#039;m going to keep that check for now. 

The real reason I&#039;m posting again tho is to share one more missing step in this cake parallel-dispatch farce.. In the CakeErrorController::_construct() they call constructClasses() (to build models) and -&gt;Component-&gt;initialize() BUT in normal dispatching,  -&gt;Component-&gt;startup($controller) is called AFTER the beforeFilter call. 

Now, from what I saw, not having this on my app didn&#039;t cause any ill effects. But that could just be a function of the Components i&#039;m using not using that event. 

So my complete workaround now is this: 
// file /app/app_error.php

    /**
     * Overload this protected method because it&#039;s called by all the built-in error methods (error404, missingAction, etc) and
     * we need to fix a sneaky little bug caused by the fact that the CakeErrorController -- while a subclass of AppController and designed
     * to function like any other page controller -- is not instantiated by the normal Dispatch process and instead has this parallel bootstrap
     * in the ErrorHandler::__construct class. Somehow this was bunged a little and some of the normal dispatch steps aren&#039;t executed.
     * At a minimum, I know it&#039;s not calling AppController::beforeFilter(). In our lil app here we use that HEAVILY so this became a big problem for us.
     *
     * @see A little discussion I participated in on the subject here: http://www.bradezone.com/2009/05/21/cakephp-beforefilter-and-the-error-error/
     * @see ErrorHandler::_outputMessage()
     */
    function _outputMessage($template)
    {
        static $virgin = true;

        if ($virgin) {


	//	$controller-&gt;beforeFilter();

            $virgin = false;

            // The commented-out methods make-up the sequence of events that occurs during normal dispatch.
            // I know for certain that we need to call beforeFilter. But as of writing this, there&#039;s no benefit of me calling the others. 
            // And I have seen at least the constructClasses call done ele
            $this-&gt;controller-&gt;beforeFilter();
            $this-&gt;controller-&gt;Component-&gt;startup($this-&gt;controller);
        }
        parent::_outputMessage($template);

    }</description>
		<content:encoded><![CDATA[<p>Also, I had the $virgin check in there because I wasn&#8217;t sure if there would be a situation where more than one error is called on that same error object. I&#8217;m still not sure if that&#8217;s the case, so rather than risk calling beforeFilter et al more than once, I&#8217;m going to keep that check for now. </p>
<p>The real reason I&#8217;m posting again tho is to share one more missing step in this cake parallel-dispatch farce.. In the CakeErrorController::_construct() they call constructClasses() (to build models) and -&gt;Component-&gt;initialize() BUT in normal dispatching,  -&gt;Component-&gt;startup($controller) is called AFTER the beforeFilter call. </p>
<p>Now, from what I saw, not having this on my app didn&#8217;t cause any ill effects. But that could just be a function of the Components i&#8217;m using not using that event. </p>
<p>So my complete workaround now is this:<br />
// file /app/app_error.php</p>
<p>    /**<br />
     * Overload this protected method because it&#8217;s called by all the built-in error methods (error404, missingAction, etc) and<br />
     * we need to fix a sneaky little bug caused by the fact that the CakeErrorController &#8212; while a subclass of AppController and designed<br />
     * to function like any other page controller &#8212; is not instantiated by the normal Dispatch process and instead has this parallel bootstrap<br />
     * in the ErrorHandler::__construct class. Somehow this was bunged a little and some of the normal dispatch steps aren&#8217;t executed.<br />
     * At a minimum, I know it&#8217;s not calling AppController::beforeFilter(). In our lil app here we use that HEAVILY so this became a big problem for us.<br />
     *<br />
     * @see A little discussion I participated in on the subject here: <a href="http://www.bradezone.com/2009/05/21/cakephp-beforefilter-and-the-error-error/" rel="nofollow">http://www.bradezone.com/2009/05/21/cakephp-beforefilter-and-the-error-error/</a><br />
     * @see ErrorHandler::_outputMessage()<br />
     */<br />
    function _outputMessage($template)<br />
    {<br />
        static $virgin = true;</p>
<p>        if ($virgin) {</p>
<p>	//	$controller-&gt;beforeFilter();</p>
<p>            $virgin = false;</p>
<p>            // The commented-out methods make-up the sequence of events that occurs during normal dispatch.<br />
            // I know for certain that we need to call beforeFilter. But as of writing this, there&#8217;s no benefit of me calling the others.<br />
            // And I have seen at least the constructClasses call done ele<br />
            $this-&gt;controller-&gt;beforeFilter();<br />
            $this-&gt;controller-&gt;Component-&gt;startup($this-&gt;controller);<br />
        }<br />
        parent::_outputMessage($template);</p>
<p>    }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shane</title>
		<link>http://www.bradezone.com/2009/05/21/cakephp-beforefilter-and-the-error-error/#comment-252</link>
		<dc:creator>Shane</dc:creator>
		<pubDate>Fri, 17 Jun 2011 19:17:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.bradezone.com/?p=549#comment-252</guid>
		<description>Again, I&#039;m glad I found other people with this same problem because I was annoyed and stumped when my 404&#039;s were covered with error messages (things not being set I set in the beforeRender). Thanks for posting your original post. It&#039;s hell stepping thru all that framework code line-by-line without even knowing really what you&#039;re looking for. 

Before i saw your post I just assumed maybe the error controller was maybe clearing the output buffer before it was flushed and then including itself and never re-calling my controller methods. Nope. I was barking up the wrong tree.</description>
		<content:encoded><![CDATA[<p>Again, I&#8217;m glad I found other people with this same problem because I was annoyed and stumped when my 404&#8242;s were covered with error messages (things not being set I set in the beforeRender). Thanks for posting your original post. It&#8217;s hell stepping thru all that framework code line-by-line without even knowing really what you&#8217;re looking for. </p>
<p>Before i saw your post I just assumed maybe the error controller was maybe clearing the output buffer before it was flushed and then including itself and never re-calling my controller methods. Nope. I was barking up the wrong tree.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brade</title>
		<link>http://www.bradezone.com/2009/05/21/cakephp-beforefilter-and-the-error-error/#comment-250</link>
		<dc:creator>Brade</dc:creator>
		<pubDate>Fri, 17 Jun 2011 13:33:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.bradezone.com/?p=549#comment-250</guid>
		<description>Aha, NOW we&#039;re getting somewhere. I&#039;m all for using the proper Cake conventions, but I&#039;m even more in favor of economy (which after all is one of the main points of OOP). Luckily your method seems to get us on this track.

Looking at the code again (in both the latest 1.2 and 1.3), I see that you&#039;re correct that _outputMessage is always called. And what&#039;s interesting is that _outputMessage includes a call to afterFilter but NOT beforeFilter. So now I&#039;m more convinced than ever that this is simply an oversight by the CakePHP developers. By merely including a call to beforeFilter at the top of this function, our problems would be solved.

Therefore we don&#039;t even need to bother with constructClasses since that is called in the CakeErrorController object created by the ErrorHandler. And I also believe we don&#039;t need the static variable check, since we&#039;re merely adding a beforeFilter call to match the afterFilter call that&#039;s already there, so I ended up with the following code in my app_error.php file:

&lt;pre&gt;
class AppError extends ErrorHandler {
    function _outputMessage($template) {
        $this-&gt;controller-&gt;beforeFilter();
        parent::_outputMessage($template);
    }
}
&lt;/pre&gt;

I see also that this is pretty close to what Georg had, only moving it into the private method that all the error methods use, so hooray for teamwork, everyone!</description>
		<content:encoded><![CDATA[<p>Aha, NOW we&#8217;re getting somewhere. I&#8217;m all for using the proper Cake conventions, but I&#8217;m even more in favor of economy (which after all is one of the main points of OOP). Luckily your method seems to get us on this track.</p>
<p>Looking at the code again (in both the latest 1.2 and 1.3), I see that you&#8217;re correct that _outputMessage is always called. And what&#8217;s interesting is that _outputMessage includes a call to afterFilter but NOT beforeFilter. So now I&#8217;m more convinced than ever that this is simply an oversight by the CakePHP developers. By merely including a call to beforeFilter at the top of this function, our problems would be solved.</p>
<p>Therefore we don&#8217;t even need to bother with constructClasses since that is called in the CakeErrorController object created by the ErrorHandler. And I also believe we don&#8217;t need the static variable check, since we&#8217;re merely adding a beforeFilter call to match the afterFilter call that&#8217;s already there, so I ended up with the following code in my app_error.php file:</p>
<pre>
class AppError extends ErrorHandler {
    function _outputMessage($template) {
        $this->controller->beforeFilter();
        parent::_outputMessage($template);
    }
}
</pre>
<p>I see also that this is pretty close to what Georg had, only moving it into the private method that all the error methods use, so hooray for teamwork, everyone!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shane</title>
		<link>http://www.bradezone.com/2009/05/21/cakephp-beforefilter-and-the-error-error/#comment-248</link>
		<dc:creator>Shane</dc:creator>
		<pubDate>Fri, 17 Jun 2011 11:02:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.bradezone.com/?p=549#comment-248</guid>
		<description>Brade, five lines of code or not, I think the reason many people are looking for a way to put this into the Error stack somewhere is that it&#039;s far more idiomatic to the framework. 

Specifically, by calling beforeFilter (and beforeRender if it&#039;s needed) in the AppController constructor means that it&#039;s going to run earlier than it normally would. You&#039;re taking a very conventional event schedule and flipping it. 

Now this isn&#039;t horrible, of course. Sometimes hacks are part of life. But if you&#039;ve got a superclass (AppController in this case) and you&#039;ve got a conditional to execute code only when that class is being inherited by a specific subclass -- that&#039;s a pretty awful code smell. I mean, that rubs against the grain of OO design. 

Still, I for one appreciate your solution and it helped my find my own solution. Which is pretty simple, actually: 

Every one of the built-in error methods call the _outputMessage() function. 

So I overloaded that: 

    function _outputMessage($template)
    {
        static $virgin = true;

        if ($virgin) {

            $virgin = false;
            $this-&gt;controller-&gt;constructClasses();
            $this-&gt;controller-&gt;beforeFilter();

        }
        parent::_outputMessage($template);

    }


Done.</description>
		<content:encoded><![CDATA[<p>Brade, five lines of code or not, I think the reason many people are looking for a way to put this into the Error stack somewhere is that it&#8217;s far more idiomatic to the framework. </p>
<p>Specifically, by calling beforeFilter (and beforeRender if it&#8217;s needed) in the AppController constructor means that it&#8217;s going to run earlier than it normally would. You&#8217;re taking a very conventional event schedule and flipping it. </p>
<p>Now this isn&#8217;t horrible, of course. Sometimes hacks are part of life. But if you&#8217;ve got a superclass (AppController in this case) and you&#8217;ve got a conditional to execute code only when that class is being inherited by a specific subclass &#8212; that&#8217;s a pretty awful code smell. I mean, that rubs against the grain of OO design. </p>
<p>Still, I for one appreciate your solution and it helped my find my own solution. Which is pretty simple, actually: </p>
<p>Every one of the built-in error methods call the _outputMessage() function. </p>
<p>So I overloaded that: </p>
<p>    function _outputMessage($template)<br />
    {<br />
        static $virgin = true;</p>
<p>        if ($virgin) {</p>
<p>            $virgin = false;<br />
            $this-&gt;controller-&gt;constructClasses();<br />
            $this-&gt;controller-&gt;beforeFilter();</p>
<p>        }<br />
        parent::_outputMessage($template);</p>
<p>    }</p>
<p>Done.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brade</title>
		<link>http://www.bradezone.com/2009/05/21/cakephp-beforefilter-and-the-error-error/#comment-210</link>
		<dc:creator>Brade</dc:creator>
		<pubDate>Sun, 05 Jun 2011 19:37:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.bradezone.com/?p=549#comment-210</guid>
		<description>It&#039;s hard to get easier than my five lines of code, but thanks for the feedback. Your solution is quite similar to Matt&#039;s at the top--again, the problem is that you&#039;d have to override a number of methods to catch all possible errors. My solution ensures that no matter the error (even the ones encountered in debug mode), your AppController variables and your template will remain intact.</description>
		<content:encoded><![CDATA[<p>It&#8217;s hard to get easier than my five lines of code, but thanks for the feedback. Your solution is quite similar to Matt&#8217;s at the top&#8211;again, the problem is that you&#8217;d have to override a number of methods to catch all possible errors. My solution ensures that no matter the error (even the ones encountered in debug mode), your AppController variables and your template will remain intact.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Georg Graf</title>
		<link>http://www.bradezone.com/2009/05/21/cakephp-beforefilter-and-the-error-error/#comment-209</link>
		<dc:creator>Georg Graf</dc:creator>
		<pubDate>Sun, 05 Jun 2011 09:48:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.bradezone.com/?p=549#comment-209</guid>
		<description>Oh that displaying didnt work well, so again:

class AppError extends ErrorHandler 
{
	function myError($params)
	{
		$this-&gt;controller-&gt;beforeFilter(); 
		$this-&gt;_outputMessage(&#039;my_error&#039;);
	}
}</description>
		<content:encoded><![CDATA[<p>Oh that displaying didnt work well, so again:</p>
<p>class AppError extends ErrorHandler<br />
{<br />
	function myError($params)<br />
	{<br />
		$this-&gt;controller-&gt;beforeFilter();<br />
		$this-&gt;_outputMessage(&#8216;my_error&#8217;);<br />
	}<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Georg Graf</title>
		<link>http://www.bradezone.com/2009/05/21/cakephp-beforefilter-and-the-error-error/#comment-208</link>
		<dc:creator>Georg Graf</dc:creator>
		<pubDate>Sun, 05 Jun 2011 09:46:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.bradezone.com/?p=549#comment-208</guid>
		<description>Hey, there&#039;s a lot easier way to deal with that problem!
Just call $this-&gt;controller-&gt;beforeFilter(); in your error method:


controller-&gt;beforeFilter(); 
		$this-&gt;_outputMessage(&#039;my_error&#039;);
	}
}	
?&gt;</description>
		<content:encoded><![CDATA[<p>Hey, there&#8217;s a lot easier way to deal with that problem!<br />
Just call $this-&gt;controller-&gt;beforeFilter(); in your error method:</p>
<p>controller-&gt;beforeFilter();<br />
		$this-&gt;_outputMessage(&#8216;my_error&#8217;);<br />
	}<br />
}<br />
?&gt;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Domnik Binz</title>
		<link>http://www.bradezone.com/2009/05/21/cakephp-beforefilter-and-the-error-error/#comment-154</link>
		<dc:creator>Domnik Binz</dc:creator>
		<pubDate>Mon, 14 Mar 2011 11:53:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.bradezone.com/?p=549#comment-154</guid>
		<description>Hey there, thanks a lot. That was a great tip!</description>
		<content:encoded><![CDATA[<p>Hey there, thanks a lot. That was a great tip!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

