This course will become read-only in the near future. Tell us at community.p2pu.org if that is a problem.

CODE: How does Lightweight MVC work?



 

So let’s properly look at the PHP code i know i have rambled alot about the MVC and how it works when to use an MVC but i wanted to make sure users understand MVC and what its for and how it works but this is lesson 5 lets properly look at some code,
 
So if you open the bootstrap.php, as i have showed you that the entry point “public_html/index.php” only loads the bootstrap.php file,
 
So as you know i have added the debugging code to the top of this file as you should currently have them activated, lets properly look at this code, 
 
you will notice that the session_start(); is called this is a php core function that enable us as developers to use the $_SESSION variable this allows you to save information for the current user on the application, this again is part of the php core and uses a cookie to save the session_id how ever this creates a problem with the new EU cookie laws coming into play on the internet your not allowed to save a cookie on the website users computer with out them explicitly say yes to you saving a cookie on the computer, but if you withing the EU you can look into using php to save the session ID on server and to the browser but i will not go into that here,
 
next we include the master controller and master module these contain base classes that need to be extended upon to utilise the basic system used in most applications in Lightweight MVC i tried to keep these as small as possible to show users that MVC’s don’t need intensive librarys to work,
 
Next we set a variable as most $_ variables in php are core handled i have to declare this one as it does not exist but accessing it not as simple as one would like but again i will cover using this is another lesson
 
the next is the main core of the MVC architecture this is the auto loader, 
function __autoload($class_name) this is a supper method of php __autoload will be called when ever a “new class();” is called and the class_name will be provided by the php core when a class is instantiated,
 
this the core of and MVC so lets look at what it does then once it’s called so with this MVC were using _ as a directory path so first we get the $class_name and we split it on the underscores so that we get a include file Path so for every part we put a / on the end E.G if we call new controller_index it will build the path controller/index.php this will then be require_once into so should the file not be found a php core error will be triggered,
 
Ok so we know how were loading a file containing a class when we call it but what about the address loaded classes E.G http://pvpmvc.local.barkersmedia.co.uk/index/index how does that address fire the method “indexAction” from the controller “controller_index” well that is handled by the next set of code “Controller::getLoadDetails($controller, $action);” now you will see where using $controller and $action but you will notice that we have not set these and that were calling a method though “::” what is this well lets start from the scratch Controller is our master controller file will i sort of cheated here and used the controller to store a function for the sake of keeping the file as tidy as possible, i used some thing call a php static member method, a static member is set of code that has been added the the memory that can’t be instanced if you were to try php will create some thing called a memory pointer to the original code hence the position in memory is static and does not move but when creating the method we have to tell php it is a static,
next is the variable i have not set but i’m passing them into the method well this is a short hand to create a variable in memory so some where to save information but this is handles in part by my calling them and php knowing they don’t exists so it creates them for me then the method i’m calling will only take a point to memory this is done in the declaration of the method i use &$var this means i’m affecting the memory passed in and not duplicating the memory then changing the duplicate values i’m changing memory where the variable php has created in memory for me using the position assigned to the variable name,
 
So this is getting a bit complex so i’m going to try to explain as simple as possible now this is a rule for all programming Lang's  think of a Peace of string each millimeter is a byte in memory now for get how complex an operating system will make memory just think this string is a 200MB stick of memory that is only used for the php script you are building nothing else gets stored here,
 
So we have functions that will take up large sections of string but then when we create a variable we are creating 2 locations in memory the first is where we store this variable name points to this start location though to this end location in the memory E.G 500mm to 600mm so where saving a 100mm or 100 byte variable and then for humans and the size of ram and that fact that an operating system uses memory we use variable by name we don’t say write this to memory from here to here as we don’t know that the memory we have used is free  and could be over righting another programs data in memory well then that application is going to hang and cause the computer to hang and crash so we let the operating system handle this for us, there are a few talented programmers that are able to read the memory and handle locking memory to there application but for the most part the programming lang's we use in this day in age we let the operating system handle it just think the computer i use for development has 12GB of ram so how much string would that be, 12 * 1024 * 1024 *1024 = 12884901888mm that would be over 128 million meters of string that’s over half the length of the uk top to bottom
 
Well back to the php code so now we under stand how we’re saving the $controller and $action (please make sure your running the latest version of Lightweight MVC) lets look at how were getting them so if you open up the controllers/controller.php and the class Controller well now if you scroll to the bottom you will see the static member method where calling getLoadDetails you will see that the &$controlerName and &$actionName,
the following line is some thing strange global $_URL well remeber i told you that i had created the $_URL in memory well that memory was not part of a class memory group so with the global declaration i’m telling php that while in this method using $_URL means i’m pointing to the declaration in the global scope so this is the reference not contained in a class but the one that is global to all my scripts, so you will notice i’m accessing the REQUEST_URI this is obtained from Apache and passed to php by Apache so we get a file path provided from the browser so the pvpmvc.local.barkersmedia.co.uk/index/index/fu/bar and we save the parts separated by the / we then then remove the first section as we don’t care about the web address used next we save the first section left index and remove it form our array this one is the controller name were loading, we do the same for the second this one contains the name of the action were calling, the rest are URL argument and we save them to the $_URL global variable as a $key => $val so the example i used $_URL would contain $_URL[‘fu’] == “bar”
 
So now you know how we have got the controller and the method text passed from the browser lets go back to the bootstrap.php now so next check to see if controller was given so if there were any url Parameters provided from the browser if not then its using the default controller_index class and indexAction as the method, 
if the controller was provided then we prepped the standard controller_ for the name and file path handled by the auto loader then we check if the action was passed if it was we append the Action to the method name so with the URL i have used as a demo and this is what would happen if we went to the address with out any params on the end of the url it auto maticly loads the controller_index and the indexAction action
 
but when we access this page we get an error well that is there purely to show what will happen when you try to load a view that does not exist to fix this if you open the controller/index.php and edit the line “$this->loadView("index/index");” to be “$this->loadView("index");” and save it and refresh your browser and you will see the welcome page, 
 
Well once you have done that leave a comment with a print screen of your browser with the working code

Task Discussion