Wednesday, September 11, 2013

Creating A Simple PHP MVC Framework Part Two (smarty version)

 Written by: Lorenzo D. Alipio  

This is part two of "Creating A Simple PHP MVC Framework (smarty version)" series. In part one, we were able to create our needed project directories. 

Htaccess file.
We need to create two .htaccess files. One for the project directory and one for web directory. Copy and paste codes below and save it as .htaccess inside the  mvcproject directory.

filename: mvcproject/.htaccess

<IfModule mod_rewrite.c>

    RewriteEngine on

    RewriteRule ^$  web/   [L]

    RewriteRule (.*) web/$1 [L]

 </IfModule>

the codes above will redirect all request to the web directory of our application, and there will no contents that can be accessed by the public.

Create another .htaccess file and save it inside the web directory.

filename: mvcproject/web/.htaccess

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteCond $1 !\.(gif|jpg|png|ico|css|js|swf|wav|mp3|less|cur) [OR]

RewriteCond %{REQUEST_URI} ^/web/.*\.css$

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

 

RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]

</IfModule>


We need to test if these .htaccess are working as intended. Create a php file called index.php, and save it to the root directory of out application.

<?php

echo 'Hello from the root index.php';

?>

Create another index.php file and save it in the web directory.       
<?php

echo 'Hello from the Web directory index.php';

?>

Direct your browser to http://localhost/mvcproject/. If everything is well, we should be reading
"Hello from the Web directory index.php".

This concludes the part two of creating our PHP MVC framework tutorial series. Next will be part three, and we will be creating bootstrap and autoloader files. See you next time and thank you for reading. 

No comments:

Post a Comment