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

Setup Lightweight MVC



Right then developers lets get a bit more advanced.

please note that this is my own project and i do believe in Open Source hence why the MVC system is on one of the best public repository's of php code on the Internet if you ever looking for something that you don't know where to start i would say check this site.

Right then lets get back to task firstly you will need to download Lightweight MVC this can be got from the links on the left hand side of this page.

while that’s downloading lets create the directory inside our Apache web root for windows users using XAMPP it will be “<install_path>\htdocs”

lets name our new directory phpmvc

once Lightweight MVC is downloaded you will need to extract it to the directory we just created.

now lets make sure that apache is running again this varys in other operating system but on windows with XAMPP if you find the XAMPP control panel in windows start menu it will be in a folder called Apache Friends once this is done make sure there is running notice next to Apache if not click the start button

ok so we now have our MVC inside our Apache web root lets try and access it.

Open your preferred browser and browse to http://localhost/phpmvc/public_html

you will notice that your browser receives a HTML 500 error working in php you might find this quit commonly it’s caused by any php error that has throw an exception and there has been no handling what so ever now inside Lightweight MVC i have added a switch to enable the standard php exception handling.

So lets have a quick look at the index.php file that were trying to load.

<?php
require("../bootstrap.php");
?>

you will see only one line of code but why this is the main file also known as the Entry Point in compiled Programming Languages so how come it’s so empty?


Well the explanation is quite simple i’m using it purely as an entry point file the MVC code is not stored where any one could see the code even if there was an error on the server and it started showing our php code no users could see the code used by the site were including the file is outside of the “public directory” but this is jumping a little ahead lets go back to where our error is occurring so we have seen that our index.php is good although almost empty.

So lets have a look at the bootstrap.php

Well now that looks more like a coded file that’s doing some thing. well to be truth full this is still a small php file only 63 lines of code some of the files i work with are over 1000 lines of code but then that’s messy and we want to keep things tidy and small for now.

Well lets get back to our error you will notice there is a block of commented out code at the top that say “## debuging disable when not needed” so lets turn this code on to do this remove the 2 #’s from the 2 lines below and save the file.

Now go back to your browser and refresh the page well would you look at that it’s not pretty but we have an error.



it’s trying to open a file that does not exists well this is down to the code in the bootstap.php most MVC follow this design pattern where they require there own domain to work don’t worry you don’t require to purchase a domain or use a hosts file hack to get this working i have already setup a loop back domain for you to use <username>.local.barkersmedia.co.uk  where user name is your P2PU user name this will always point to your own local copy of Apache it works by using domain server that then provide the IP address 127.0.0.1 (this is know as your loop-back address this means it points back to your own computer ) .

So ok we have the MVC in a Directory that Apache can access we have our domain that we can use so how do we set up our domain to point to our file well this is done via the Apache configuration i can guide the users on windows using XAMPP for user of MAC or Linux you will need to find a tutorial to tell you were your editing.

So on the you need to browse to a directory inside your XAMPP install path
“<install_path>\apache\conf\extra\”.

Once in that directory you need to open the httpd-vhosts.conf file in a text editor i notepad will do it but i would recommend you download notepad++ then you can right click and click “Edit with notepad++”.

first we need to make sure virtual host support is enable so on line 19 there is “NameVirtualHost *:80” make sure this is not a # in front of that line if there is remove it.

Secondly we need to enable the virtual host for our domain and point it to the correct folder

<VirtualHost *:80>
DocumentRoot "<install_path>/htdocs/phpmvc/public_html"
ServerName <username>.local.barkersmedia.co.uk
</VirtualHost>

then DocumentRoot says where the folder for the site is but notice how were using the public_html directory of our MVC this is due to the security that i told you about earlier where no code is kept in a place were a public user can access it. Dont forget to change your ServerName to match your username if this is your first Virtual host then here is setup i always add to to catch any access though a different domain to be caught on to the main local host page

<VirtualHost *:80>
DocumentRoot "c:/xampp/htdocs/"
ServerName localhost
ServerAlias *
</VirtualHost>

So once you have set this up we need Apache to see the changes to do this we have to restart Apache, so open the XAMPP control panel from your start menu it’s under the Apache friends directory once you have it open click stop next to Apache wait for it to change to a start button and click it again once you have done this you should be able to browse to http://phpmvc.local.barkersmedia.co.uk/ You should notice that you see a page that looks like a 404 File not found error but if you look closely you will notice this error is thrown by Lightweight MVC the clue is in the bottom of the page if you see this well done everything has worked just take a screen shot of the page and upload it for me to see.

What this does is tell Apache that on any IP address * = wildcard (Basicly any thing)

Task Discussion