<?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/"
	>

<channel>
	<title>iamdave &#187; 404</title>
	<atom:link href="http://i.amdave.com/archives/tag/404/feed" rel="self" type="application/rss+xml" />
	<link>http://i.amdave.com</link>
	<description>Programmer - Baltimore, MD</description>
	<lastBuildDate>Mon, 10 May 2010 14:06:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Kohana v3 :: Custom 404 Page</title>
		<link>http://i.amdave.com/archives/32</link>
		<comments>http://i.amdave.com/archives/32#comments</comments>
		<pubDate>Wed, 28 Oct 2009 17:02:37 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Kohana]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[404]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://i.amdave.com/?p=32</guid>
		<description><![CDATA[A best practice for a web app/site in &#8220;production&#8221; is have an error page that looks like its part of your website &#8211; kind of a friendly error page. With Kohana v3 this is fairly easy to do, and only requires one new file, and a modification to one other file. Note: This solution was [...]]]></description>
			<content:encoded><![CDATA[<p>A best practice for a web app/site in &#8220;production&#8221; is have an error page that looks like its part of your website &#8211; kind of a friendly error page.  With Kohana v3 this is fairly easy to do, and only requires one new file, and a modification to one other file.</p>
<p><em>Note: This solution was found in the <a href="http://forum.kohanaphp.com/comments.php?DiscussionID=3754" target="_blank">Kohana Forums.</a></em><br />
<strong></strong></p>
<p><strong>Step 1 : application/boostrap.php</strong></p>
<p>Define &#8216;IN_PRODUCTION&#8217; in the Environment Setup Section</p>
<div style="background-color:#eeeeee; padding:10px;"><code><br />
define('IN_PRODUCTION', true);<br />
</code></div>
<p>Catch the Exception coming from any invalid Request</p>
<p>After you define all your Routes, Kohana executes the request, sends the headers and renders the view.  If you haven&#8217;t already made changes, it looks like this:</p>
<div style="background-color:#eeeeee; padding:10px;"><code><br />
echo Request::instance()<br />
-&gt;execute()<br />
-&gt;send_headers()<br />
-&gt;response;<br />
</code></div>
<p>Here is the replacement you should make, with comments to explain:</p>
<div style="background-color:#eeeeee; padding:10px;"><code>// Instantiate your Request object<br />
$request = Request::instance();<br />
// The give it a try, to see if its a valid request<br />
try<br />
{<br />
$request-&gt;execute();<br />
}<br />
catch (Exception $e)  // if its not valid, it gets caught here<br />
{<br />
if (! IN_PRODUCTION)  // if this is Development, its displays the error<br />
{<br />
throw $e;<br />
}<br />
// if its IN_PRODUCTION, it does the following:<br />
// Logs the error<br />
Kohana::$log-&gt;add(Kohana::ERROR, Kohana::exception_text($e));<br />
// Marks the status as 404<br />
$request-&gt;status = 404;<br />
// Renders the View for your CUSTOM 404 of your choice<br />
$request-&gt;response = View::factory('public/templates/main') //your view may be different<br />
-&gt;set('title', '404')<br />
-&gt;set('content', View::factory('errors/404'));  //again, your view may be different<br />
}<br />
// then continues on with the request process to display your custom 404 page<br />
$request-&gt;send_headers()-&gt;response;<br />
echo $request-&gt;response;</code></div>
<p><strong>Step 2 : Create A Custom Error View</strong></p>
<p>Create a new folder in your views folder called &#8220;Kohana&#8221;</p>
<p>Create a file in that folder called &#8220;error.php&#8221;</p>
<p>Whatever you put in your application/views/Kohana/error.php file will take precedents over Kohana&#8217;s views/Kohana/error.php file, so this is where I styled my error page to look like the rest of my public application.  I copied much of the error reporting code directly from Kohana&#8217;s error file and used a conditional to display it in development or display a friendlier message in production.  See the code skeleton:</p>
<div style="background-color:#eeeeee; padding:10px;"><code><br />
if (IN_PRODUCTION) ///defined in boostrap.php<br />
/* Render your friendly error message */<br />
else<br />
/* Render Kohana's error which you copied from views/Kohana/error.php */<br />
</code></div>
]]></content:encoded>
			<wfw:commentRss>http://i.amdave.com/archives/32/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
