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?
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:
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.
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:
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”.
Lets add this into a real life scenario and add the following controller in the file “framework/application/controller/Invoice.php”
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.