Controllers are the fundamental starting point of the Boiler framework, linking the raw HTTP request to the correct function. The following tutorial builts a “Hello World” controller, which you can run on your own machine.

[For the purposes of this tutorial, we will assume you are running on localhost]

When you first open Boiler using http://localhost/ You will notice a message which says something along the lines of “I’m using in total 0.7MB of memory :)”. This is the amount of memory since the index script loaded on Line 1. So how did we get to this?

Controller Routing

Controllers are routed through the Router (can be overridden through framework/application/core/Router.php, and found in the core files in framework/system/core/Router.php). This file splits the URL through the slashes and routes it accordingly. You will also notice in the system files that the default controller is “Home” and the default function is “index”. Let’s have a look at the following URL and see how it is processed:

/home/index/123/456

The first attempt the router does is trying to make the last part of the url a function, the part before a class, and the parts before that namespaces. This would effectively try and call the following code.

  1. include "framework/application/controller/home/index/123.php";
  2.  
  3. $controller = new \Controller\Home\Index\123();
  4.  
  5. $controller->456();

Of course, the following code does not work, therefore the system proceeds down the url to make “home” the namespace, index” the class, 123 the function, and 456 the argument to the function.

This would then continue looping down until we achieve the following effective code:

  1. include "framework/application/controller/Home.php";
  2.  
  3. $controller = new \Controller\Home();
  4.  
  5. $controller->index(123, 456);

Which can be called! Check out the controller yourself! Now try the URL http://localhost/home/index/123/456!

Next try out the URL http://localhost/. We can see here that there is no path, therefore we automatically assume the default controller and default function!

Finally try out the URL http://localhost/home. Here you can see we are using the specified controller “\Controller\Home”, but using the default function “index”.

Real Life Scenario

Lets add this into a real life scenario and add the following controller in the file “framework/application/controller/Invoice.php”

  1. namespace Controller;
  2.  
  3. class Invoice {
  4. function index($id=false) {
  5. if ($id == false) {
  6. echo "Index of all invoices";
  7. } else {
  8. echo "Invoice: $id";
  9. }
  10. }
  11. }

Here you can see the power of the URL system. Try going to http://localhost/invoice/ and http://localhost/invoice/1 to see how the URL is being routed through the system.


Add Comment | Categories: Boiler Framework, Uncategorized | Posted: October 11, 2012

Leave a Reply

Your email address will not be published. Required fields are marked *


× one = 3

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>