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

MVC when its used on a server



 

So we now how a setup of Lightweight MVC and an understanding of what an MVC is so lets have a deep look at how Lightweight MVC entry point works as i have explained the entry point for the Lightweight MVC is the index.php but this will work if the server address is /index.php or if a file to load is not provided. so where using a module of Apache Called mod-rewrite for IIS users there is a software you can purchases that will allow mod-rewrite commands to work.
Apache uses “.htaccess” files for rules used by hierarchical this means if you had a root folder with a “.htaccess” inside it it will also affect any child folders or pages.
 
so lets have a look at the “.htaccess” Lightweight MVC uses
 
Options +FollowSymlinks
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
 
So what have we got Well we have 5 different Apache commands
The First one has nothing to do with mod-rewrite it is a command that works on linux or Mac windows support it flaky as windows only just got symlinks working properly from windows Vista if i remember correctly,
 
The next line is telling Apache that mod-rewrite needs to be active
then we are saying what file paths are to be used as part of the rewrite so / means any thing in and below the containing folder
 
RewiteCond is a condition that needs to be matched for the first rule it occurs as we’re only using one Rule both Cond’s are working for that rule !-f means is not a file and !-d means its not a directory so basically were checking that the file does not exists so that if it does the browser can still access it this is used for javascript, image, css and other files sent directly to the browser, then any thing that does not exists were redirecting to the index.php for that is our MVC entry point.
 
So let’s have a look at loading a css file in the browser
 
in your text editor create a file in path public_html/css/ called test.css now remember that any files downloaded to the browser have to be in the Apache public directory as the browser does not know the servers lay out or understand that its using an MVC it is for programmed code only so on a php server only the server runs the php code and can understand the files structure.
 
well in our CSS file lets place a comment so just use
/* this is a css comment in a css file not loaded though the MVC */
once you have done this you will need to access that file so browse to the file
 
this should now look like a text file with your comment shown in the browser.
 
once you have done this leave a print screen in the comments

Task Discussion