<?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>Adventures in Keyframes and Code &#187; Kohana</title>
	<atom:link href="http://www.keyframesandcode.com/code/category/development/php/kohana/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.keyframesandcode.com/code</link>
	<description>MaxScript, ActionScript, PHP &#38; JavaScript</description>
	<lastBuildDate>Mon, 30 Aug 2010 15:12:36 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Documentor &#8211; PHP to HTML to Redmine Wiki syntax converter</title>
		<link>http://www.keyframesandcode.com/code/development/php/documentor/</link>
		<comments>http://www.keyframesandcode.com/code/development/php/documentor/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 17:23:54 +0000</pubDate>
		<dc:creator>Dave Stewart</dc:creator>
				<category><![CDATA[Kohana]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.keyframesandcode.com/code/?p=204</guid>
		<description><![CDATA[Overview

I&#8217;ve released what I think is a pretty robust version of Documentor, my PHP &#62; HTML &#62; Redmine documentation app.
These are some of the nice features to make it in:
PHP

Classes, methods and properties are now supported
Excellent JavaDoc &#38; commenting conversion
Automatic grouping of properties based on comments structure

HTML

Full or partial HTML generation, with semantic markup, anchors [...]]]></description>
			<content:encoded><![CDATA[<h3>Overview</h3>
<div id="CommentBody_22508" class="CommentBody">
<p>I&#8217;ve released what I think is a pretty robust version of <a href="/resources/php/redmine/documentor/">Documentor</a>, my PHP &gt; HTML &gt; Redmine documentation app.</p>
<p>These are some of the nice features to make it in:</p>
<p><strong>PHP</strong></p>
<ul>
<li>Classes, methods and properties are now supported</li>
<li>Excellent JavaDoc &amp; commenting conversion</li>
<li>Automatic grouping of properties based on comments structure</li>
</ul>
<p><strong>HTML</strong></p>
<ul>
<li>Full or partial HTML generation, with semantic markup, anchors and full CSS support</li>
<li>Multiple structural options</li>
<li>Generation of sample code</li>
</ul>
<p><strong>Wiki format</strong></p>
<ul>
<li>Automatic conversion of almost all supported tags, including simple tables</li>
<li>Automatic link generation for external and internal links</li>
</ul>
<p><strong>UI</strong></p>
<ul>
<li>Elegant and easy-to use AJAX interface which remembers your settings between sessions</li>
<li>Sample code example included to quickly test all the features</li>
<li>Live preview of HTML results</li>
</ul>
<h3>Check it out</h3>
<p>It&#8217;s now got its own project page, so you can file issues or suggestions there:</p>
<p><a href="http://dev.kohanaphp.com/projects/documentor/wiki/">http://dev.kohanaphp.com/projects/documentor/wiki/</a></p>
<p>And the application is here to tinker with:</p>
<p><a href="/resources/php/redmine/documentor/">http://www.keyframesandcode.com/resources/php/redmine/documentor/</a></p>
<p>I think it&#8217;s fairly bullet-proof, but please get in there and test the latest version, let me know what you like, and if you find any bugs.</p></div>
<p><a href="http://www.keyframesandcode.com/resources/php/redmine/documentor/">Check it out online and have a play</a> &#8211; I&#8217;ve tried to make it super easy, and there&#8217;s <strong>sample code</strong> you can use to get you started. Also, try copying and pasting some of the code Kohana classes in to see what happens!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.keyframesandcode.com/code/development/php/documentor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Kohana: Default controller for serving static pages</title>
		<link>http://www.keyframesandcode.com/code/development/php/kohana/kohana-default-controller-for-serving-static-pages/</link>
		<comments>http://www.keyframesandcode.com/code/development/php/kohana/kohana-default-controller-for-serving-static-pages/#comments</comments>
		<pubDate>Thu, 11 Sep 2008 14:28:03 +0000</pubDate>
		<dc:creator>Dave Stewart</dc:creator>
				<category><![CDATA[Kohana]]></category>

		<guid isPermaLink="false">http://www.keyframesandcode.com/code/?p=199</guid>
		<description><![CDATA[A brief overview of the problem
A framework such as Kohana is really useful when serving lots of dynamic pages, as it allows you organise both your thoughts and your code into folders, creating order from what could potentially be chaos, at the expense of having to adhere to a structured way of working, in this [...]]]></description>
			<content:encoded><![CDATA[<h3>A brief overview of the problem</h3>
<p>A framework such as Kohana is really useful when serving lots of dynamic pages, as it allows you organise both your thoughts and your code into folders, creating order from what could potentially be chaos, at the expense of having to adhere to a structured way of working, in this case setting up controllers and  methods that are correctly mapped from URLs (routes).</p>
<p>Whilst this is necessary for dynamic pages that need access to models, helpers and such like, for static pages that literally just need to be output to the browser, it&#8217;s quite a lot of overhead, and your controllers can very quickly end up bloated with itty-bitty &#8220;view this page&#8221; methods.</p>
<p>What would be great would be some magic method to automatically handle static pages, without having to set up an actual controller and method, that way all you really need to create is the view file, and you leave your controllers folder nice and lean.</p>
<p><em>Note: this tutorial is for Kohana 2.2. I haven&#8217;t investigated how it will work with 2.3 and the routing yet. Please comment if you have something to add!</em></p>
<h3>The Fallback controller</h3>
<p>The solution is using a default or &#8220;fallback&#8221; controller that takes care of serving views from routes that don&#8217;t map to an existing controller and method.</p>
<p>Essentially, this new controller kicks in just after the routing has drawn a blank, locates the appropriate view file, then serves it &#8211; instead of serving a 404 page.</p>
<h3>Step-by-step</h3>
<p>Let&#8217;s a take a look at how this works in a step-by-step approach from Kohana&#8217;s perspective. Don&#8217;t worry &#8211; it&#8217;s actually pretty simple, I&#8217;ve just mapped it out specifically so you can see what&#8217;s happening every step of the way.</p>
<p><strong>Let&#8217;s start with a route</strong></p>
<ul>
<li>The user navigates to a route, lets say <strong>/about/company/history</strong></li>
<li>Kohana looks for
<ul>
<li>an <strong>about</strong> controller, with</li>
<li>a <strong>company</strong> method, and intends to supply it with</li>
<li>a <strong>history</strong> argument</li>
</ul>
</li>
</ul>
<p>Here&#8217;s the thing though, because we did&#8217;t want the hassle of setting up ALL our controllers with methods just to load views, our <strong>about</strong> controller doesn&#8217;t have a <strong>company</strong> method! So what is Kohana to do?</p>
<p><strong>Let&#8217;s load the Fallback Controller!</strong><br />
Well, normally, a 404 page would be served, but using the magic of <a href="http://docs.kohanaphp.com/general/hooks">hooks</a>, we&#8217;re going to give Kohana one last chance to do something before serving that horrid 404 page.</p>
<p>So:</p>
<ul>
<li>The <a href="http://docs.kohanaphp.com/general/events#system.post_routing">system.post_routing</a> hook kicks in, and due to the way we&#8217;ve set up the hook file</li>
<li>It loads our <strong>Fallback controller</strong>, which is designed to do something useful with the route before bombing out.</li>
</ul>
<p><strong>Let&#8217;s have the Fallback Controller do it&#8217;s stuff and find the view!</strong><br />
In this case, we want it to attempt to find a view, so:</p>
<ul>
<li>The Fallback controller builds a path from the arguments <strong>/about</strong> that will (hopefully) map to a view, in this case <strong>views/main/about.php</strong>, then</li>
<li>Attempts to find this view using <a href="http://docs.kohanaphp.com/core/kohana#finding_files">Kohana::find_file</a>, and
<ul>
<li>if found &#8211; <strong>loads the view!</strong></li>
<li>if not found &#8211; serves the 404 page</li>
</ul>
</li>
</ul>
<h3>Summary</h3>
<p>Pretty useful, huh? Basically all that happened is</p>
<ul>
<li>Kohana couldn&#8217;t find a controller, so loaded a Fallback controller</li>
<li>The fallback controller took the route and found a view</li>
<li>Kohana loads the view (or the 404 page if the view didn&#8217;t exist)</li>
</ul>
<p>OK, perhaps it&#8217;s time to look at some code</p>
<h3>Code</h3>
<p>You will need to create or edit code in 3 separate places:</p>
<ol>
<li>Enable hooks in your application&#8217;s configuration file</li>
<li>Create a hook file which tells Kohana what to do in the system.post_routing event</li>
<li>Create the actual Fallback Controller that will do the finding and loading of your view files</li>
</ol>
<p>.</p>
<p><strong>application/config/config.php</strong> &#8211; Enable hooks in the main config file&#8230;</p>
<pre>$config['enable_hooks'] = TRUE;</pre>
<p>.</p>
<p><strong>application/hooks/fallback_page.php</strong> &#8211; Add a hook file to the hooks directory&#8230;</p>
<pre>Event::add('system.post_routing' ,'call_fallback_page');

function call_fallback_page()
{
    if (Router::$controller === NULL)
    {
        Router::$controller = 'fallback_page';
        Router::$arguments = Router::$segments;
        Router::$controller_path = APPPATH.'controllers/fallback_page.php';
    }
}</pre>
<p>.</p>
<p><strong>application/controllers/fallback_page.php</strong> &#8211; Set up a controller to do the business</p>
<pre>class Fallback_Page_Controller extends Page_Controller // page controller is my standard page template
{

    public function __construct()
    {

    // the constructor can be omitted if you're not doing anything special,
    // as the parent constructor it is called automatically
        parent::__construct();
    }

    public function __call($function, $arguments)
    {

    // search for a view, and load it if it exists
    // it's up to you how you handle the mapping of arguments to folders here!
        $route =  implode('/', $arguments);
        if(Kohana::find_file('views/main', $route))
        {
            $this-&gt;template-&gt;content = new View('main/'.$route);
        }

    // display alternative content if not found
        else
        {
            $view = new View('common/404');
            $view-&gt;info = array($function, $arguments);
            $this-&gt;template-&gt;content = $view;
        }
    }

}</pre>
<h3>Conculsion</h3>
<p>So now you should be able to set up a bunch of views, but needn&#8217;t worry about building controllers for them if nothing exciting is happening.</p>
<p>The fallback controller will run, will attempt to find a matching view, and if found, will echo it the page. The main thing is &#8211; it keeps your controllers folder and classes nice and lean, and you only need to build controllers that actually DO any controlling!</p>
<p>Nice.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.keyframesandcode.com/code/development/php/kohana/kohana-default-controller-for-serving-static-pages/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Kohana &#8216;filesystem&#8217; Helper</title>
		<link>http://www.keyframesandcode.com/code/development/kohana-filesystem-helper/</link>
		<comments>http://www.keyframesandcode.com/code/development/kohana-filesystem-helper/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 12:49:24 +0000</pubDate>
		<dc:creator>Dave Stewart</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Kohana]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.keyframesandcode.com/code/?p=198</guid>
		<description><![CDATA[Kohana is just an excellent framework to work with, but one of my beefs is that the structure of its bundled libraries and helpers is a little haphazard, missing basic functions you&#8217;d expect to find in the core (so you can rely on them when writing modules) omitted!
For this reason, here&#8217;s my attempt at a [...]]]></description>
			<content:encoded><![CDATA[<p>Kohana is just an excellent framework to work with, but one of my beefs is that the structure of its bundled libraries and helpers is a little haphazard, missing basic functions you&#8217;d expect to find in the core (so you can rely on them when writing modules) omitted!</p>
<p>For this reason, here&#8217;s my attempt at a more integrated helper class. It&#8217;s somewhat of a Frankenstein&#8217;s monster with bits borrowed from Code Igniter, and my own code thrown in as well. Apart from this, it:</p>
<ul>
<li>Adds a few new methods</li>
<li>Improves on some existing methods</li>
<li>Combines the &#8216;download&#8217; helper functionality</li>
<li>Tidies up various method calls</li>
</ul>
<p>The methods are:</p>
<ul>
<li>map()</li>
<li>get_folders()</li>
<li>get_files()</li>
<li>delete_files()</li>
<li>read_file()</li>
<li>write_file()</li>
<li>make_path()</li>
<li>download()</li>
</ul>
<p>Download <a href="http://www.keyframesandcode.com/resources/php/kohana/helpers/filesystem.zip">filesystem.php</a> here.</p>
<p><span id="more-198"></span></p>
<h3 style="margin-top: 30px !important;">map<span style="font-weight:normal"> ($path, $levels = NULL, $structured = FALSE, $files_first = FALSE)</span></h3>
<p><strong>Returns a hierarchical array of folders and files from the specified path.</strong></p>
<p>The parameters are</p>
<ul>
<li><strong>path</strong> : <em>string</em> &#8211; path to the folder</li>
<li>[<strong>levels</strong>] : <em>number</em> &#8211; limit how many folders deep you want to recursively scan</li>
<li>[<strong>structured</strong>] : <em>boolean</em> &#8211; split each returned branch into <em>&#8216;/folder&#8217;</em> and <em>&#8216;/files&#8217;</em> sub-arrays</li>
<li>[<strong>files_first</strong>] : <em>boolean</em> &#8211; if not returning as a structured array, list files before folders</li>
</ul>
<h3 style="margin-top: 30px !important;">get_folders<span style="font-weight:normal"> ($path, $appendPath = false)</span></h3>
<p><strong>Returns an array of folders from the specified </strong><strong>path</strong><strong>.</strong></p>
<p>The parameters are</p>
<ul>
<li><strong>path</strong> : <em>string</em> &#8211; path to the folder</li>
<li>[<strong>append_path</strong>] : <em>boolean</em> &#8211; append the original path to the return output</li>
</ul>
<h3 style="margin-top: 30px !important;">get_files<span style="font-weight:normal"> ($path, $mask = NULL, $appendPath = false)</span></h3>
<p><strong>Returns an array of files from the specified </strong><strong>path</strong><strong>.</strong></p>
<p>The parameters are</p>
<ul>
<li><strong>path</strong> : <em>string</em> &#8211; path to the folder</li>
<li>[<strong>mask</strong>] : <em>mixed</em> &#8211; a file mask to determine which files to return. Can be either
<ul>
<li><em>null</em> &#8211; all files</li>
<li><em>string</em> &#8211; a single file extension</li>
<li><em>array</em> &#8211; an array of file extensions</li>
<li><em>regular expression</em> &#8211; any valid regular expression, for example &#8216;/^config/&#8217;</li>
</ul>
</li>
<li>[<strong>append_path</strong>] : <em>boolean</em> &#8211; append the original path to the return output</li>
</ul>
<h3 style="margin-top: 30px !important;">delete_files<span style="font-weight:normal"> ($path, $mask = NULL, $del_dir = FALSE, $del_root = FALSE)</span></h3>
<p><strong>Recursively deletes all files and optionally folders from the specified </strong><strong>path</strong><strong>.</strong></p>
<p>The parameters are</p>
<ul>
<li><strong>path</strong> : <em>string</em> &#8211; path to the folder</li>
<li>[<strong>mask</strong>] : <em>mixed</em> &#8211; a file mask to determine which files to delete. Can be either
<ul>
<li><em>null</em> &#8211; all files</li>
<li><em>string</em> &#8211; a single file extension</li>
<li><em>array</em> &#8211; an array of file extensions</li>
<li><em>regular expression</em> &#8211; any valid regular expression, for example &#8216;/^config/&#8217;</li>
</ul>
</li>
<li>[<strong>del_dir</strong>] : <em>boolean</em> &#8211; delete any directories encountered when prcoessing</li>
<li>[<strong>del_root</strong>] : <em>boolean</em> &#8211; delete the root directory after processing</li>
</ul>
<h3 style="margin-top: 30px !important;">read_file<span style="font-weight:normal"> ($file)</span></h3>
<p><strong>Opens the specified </strong><strong>file </strong><strong>and returns its contents as a string.</strong></p>
<p>The parameters are:</p>
<ul>
<li><strong>file</strong>: <em>string</em> &#8211; path to the folder</li>
</ul>
<h3 style="margin-top: 30px !important;">write_file<span style="font-weight:normal"> ($path, $data, $append = FALSE)</span></h3>
<p><strong>Writes data to the specified </strong><strong>file</strong><strong>.</strong></p>
<p>The parameters are</p>
<ul>
<li><strong>path</strong> : <em>string</em> &#8211; path to the folder</li>
<li><strong>data</strong> : <em>string</em> &#8211; the data you want written to the file</li>
<li>[<strong>append</strong>] &#8211; <em>boolean</em> &#8211; optionally append data to the file, rather than overwriting it</li>
</ul>
<h3 style="margin-top: 30px !important;">make_path<span style="font-weight:normal"> ($path, $permissions = 0755)</span></h3>
<p><strong>Recursively create folders to form the specified path.</strong></p>
<p>The parameters are</p>
<ul>
<li><strong>path</strong> : mixed &#8211; path or an array of paths</li>
<li>[<strong>permissions</strong>]: <em>number</em> &#8211; permissions you want granted to the new folders</li>
</ul>
<h3 style="margin-top: 30px !important;">download<span style="font-weight:normal"> ($filedata, $filename = NULL)</span></h3>
<p><strong>Download file content by popping up a download dialog in the user&#8217;s browser.</strong></p>
<p>The parameters are</p>
<ul>
<li><strong>filedata</strong> : <em>mixed</em> &#8211; can either be
<ul>
<li><em>filepath</em> &#8211; the path to an existing file</li>
<li><em>string</em> &#8211; data you want downloaded as a file</li>
</ul>
</li>
<li>[<strong>filename</strong>] : <em>string</em> &#8211;  a filename that will appear in the user&#8217;s download dialog</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.keyframesandcode.com/code/development/kohana-filesystem-helper/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
