Megan PHP Framework
Overview
This is a demonstration framework that I created for building web pages, implemented in PHP. I started the implementation in 2015 and I update it periodically. More documentation and the entire source code may be found on github. Check the Examples menu above for various demonstrations.
Megan is the name of my youngest daughter.
Framework
- 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
phpfile 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_filesdirective 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_ROOTis the directory containing the framework. If the URI isxxx/yyy/zzzthen the path to the class file isFW_ROOT/controllers/xxx/yyy/zzz-controller.phpand 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.phpand the class name isXxxYyyZzzModel. Similarly the controller may invoke a view. The path to the class file isFW_ROOT/views/xxx/yyy/zzz-views.phpand the class name isXxxYyyZzzView. - upporting classes are found similarly. For example, the
Cookiesclass is inFW_ROOT/classes/Cookies.php. - The PHP
autoloaderfeature is used to find the class files, removing the need for explicitrequireorincludestatements. - The MVC classes rely heavily on object-oreiented class inheritance.
For example the
Viewclass does most of the work for generating a web page. Pages like theloginpage are implemented by theLoginViewclass which extendsView.
Database
- The framework supports CRUD database operations. Originally it
used
mysqlbut I recently switched tomariadbwith few changes required. - There is a singleton
DbConnclass that allows multiple classes within a page to share a common DB connection.
Presentation
- The front end code is built upon Bootstrap for CSS/presentation and jQuery for javascript/interactivity. In this day and age modern front end projects tend to be built on a javascript framework such as
AngularorReactbut I find these are overkill for a relatively simple set of web pages such as this site.