A simple MVC framework for building web pages, implemented in PHP. I have started developing it in 2015 to collect a variety of best practices that I developed over a decade or so of PHP development. I picked it up again recently to refresh my skills.
- The project is completely self-contained and does not rely on any framework (Laravel, Zend, Symfony, Cake, etc.). Why write it all from scratch? It's more fun and you learn more that way!
- There is only a single
php
file in thedoc root
, it isindex.php
, and this file only contains a few lines that invoke the framework (fw.php
) which is located outside the document root for better isolation. - The framework relies on the NGINX
try_files
directive to send all URIs which don't correspond to an existing file to theindex.php
. - The framework supports a
model-view-controller (MVC)
design pattern. The control logic is in thecontroller
, the data comes from themodel
, and the presentation of the web page is handled in theview
. - The framework parses the URI into one or more directory names and the
file name, which corresponds to a PHP class name.
For example:
FW_ROOT
is the directory containing the framework. If the URI is/xxx/yyy/zzz
then the path to the class file isFW_ROOT/controllers/xxx/yyy/zzz-controller.php
and the class name isXxxYyyZzzController
. Depending on the controller, it may invoke a model. The path to the class file isFW_ROOT/models/xxx/yyy/zzz-model.php
and the class name isXxxYyyZzzModel
. Similarly the controller may invoke a view. The path to the class file isFW_ROOT/views/xxx/yyy/zzz-views.php
and the class name isXxxYyyZzzView
. - Supporting classes are found similarly. For example, the
Cookies
class is inFW_ROOT/classes/Cookies.php
. - The PHP
autoloader
feature is used to find the class files, removing the need for explicitrequire
orinclude
statements. - The MVC classes rely heavily on object-oreiented class inheritance.
For example the
View
class does most of the work for generating a web page. Pages like the/login
page are implemented by theLoginView
class which extendsView
.
- The framework supports CRUD database operations. Originally it
used
mysql
but I recently switched tomariadb
with few changes required. - There is a singleton
DbConn
class that allows multiple classes within a page to share a common DB connection.