Create your own PHP framework using MVC design pattern (Part – 2)

Ok!, at last I have finished creating my own MVC PHP framework. I named is Sp!ce (spice). I think you guys will get lot’s of fun using this framework. So let’s start.

If you didn’t read Part-1 then please do that before continue.

Step 1: Folder Structure

I think folder structure is really important. So we need to fix that first. In my opinion, ROOT folder need to settle down with these following folders


Step 2: We are now create the .htaccess file. In this file we will use `mod_rewrite.c` apache module and need to prevent file browsing option by using “Options -Indexes”. So our .htaccess file should be look like this

click here to see .htaccess file

Step 2: Now we need to create our index.php file. In this file all we need to load controller, model and view classes and off course configuration and start up file.

click here to see index.php file

Step 3: Now you can see I have created a folder system because this folder will collect all the system files like config.php file along with model, view, controller classes that I have created. startup.php  is holding a function that will check every URL that pass through by browser and let the system know which controller will need to fetch and pass. Before digging more into that we will need to know more about the config.php file. We need to set the database related global variables like database host name, user name, password and the server host.

click here to see config.php file

Step 4: Our last goal is to create a startup.php file. This is the file where each and every URL that need to pass to the browser via index.php file. This file will check whether it goes to custom controller or default controller.

click to see startup.php file

Step 5: Download the full source code. I have created a small project named ‘addressbook‘ by using spice framework. There is also a mysql database dump file. You need to  process these follow these steps:

click to download full source code

1. Go to phpmyadmin and create a database named `spice`;
2. Now import the `sql/spice.sql` file to the database.
3. Go to system/config file. You need to set base url like `http://localhost/spice/` or what you might feet.
4. Now set the database host. Usually it’s `localhost`, database name is `spice` or the name you have created, the user is the database user name and set the password.

Your done!

GO TO the browser and then visit your site.

Let me know if you have any confusion, comments or feedback. I will really appreciate that to improve the framework. If anyone wants to contribute please click here.

Read this book Learning PHP, MySQL, JavaScript, CSS & HTML5: A Step-by-Step Guide to Creating Dynamic Websites to learn more on PHP.

Enjoy 🙂

[PHP] How to get longest string from array ?

Hope it will save your time. Sometimes in PHP we may need to find the longest string from an array. So you can get this solutions in google but I made a function to make it more usable. It will return maximum lenght, key and the string form that array.

Clieck Here to see the code

Don’t forget to share!.


[php] use echo with parameters

Have you ever seen that echo can be used with parameters????
Oya!! This can be happen like:

$a = "Tanveer";
$b = "good at nothing";
echo "Hi my name is ",$a,". I am ",$b;

Isn’t it so Funny!!!

[PHP] Dynamic function calling

People says PHP is flexible, Yes, really it is! Suppose you need to call function dynamical order. Here it is how?
The magic PHP function called call_user_func()

function increment(&$var)

$a = 0;
call_user_func('increment', $a);
echo $a."\n";

call_user_func_array('increment', array(&$a)); // You can use this instead before PHP 5.3
echo $a."\n";

Output will be :
0 1

%d bloggers like this: