Update: I rewrote the code for the framework and added page title generation as well, take a look at part 2.
After building and digging through the source of the simple MVC framework provided by Anant Garg, I was able to discover the true nature of the MVC framework. Actually, I’m able to modify his framework to an even simpler framework which just use View and a little Controller in the MVC (Model-View-Controller) with no database connectivity.
The primary use for the MVC pattern is to work with the database. However, sometimes we don’t need to use database to creating a web site. According to my own knowledge, the Model is used to interact with the database and the Controller uses logical to process the database.
I stated that I need to use “a little” Controller because it also controls which page to display and sends it to the View.
In this simple framework (and perhaps all of the MVC frameworks), the most important file is .htaccess which controls the URL. Here is the content of the file copied directly from Anant’s post:
Note that this .htaccess file needs to place in th root directory of your web site instead of public folder because we will be creating a framework that has no directory. You can create public folder or webroot folder if you want, but make sure to change the following code accordingly.
The above code converts the URL path to the url query. For example, the URL http://localhost/mvc/test would be converted to http://localhost/index.php?url=mvc/test.
Next step is to create the index.php file which used to process the url query:
This is what I called “reverse include”, because I had always been include header and footer manually on every page until I found CakePHP MVC framework.
I also stated that this framework will not have any folder. Well, to include header and footer, you need to create an includes folder to place all the repeatable scripts, unless you really want to be disorganized .
Congratulation, you just created a mini framework. Note that this framework is used for small websites or personal sites where the database is not used at all. This means that it doesn’t have a back-end to edit the page directly on the server.
However, there is also an advantage to it. You don’t need to include the header and footer manually when creating a new page. It will include them automatically. It also can direct to the custom page if the specified page is not found.
About the title issue: I haven’t found a solution to set the title on individual page yet. Right now the title in the header is static. I will update this post once I find a solution to this issue.
Feel free to discuss this framework with me in the comment below.