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
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 isxxx/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
. - upporting 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 thelogin
page are implemented by theLoginView
class which extendsView
.
Database
- 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.
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
Angular
orReact
but I find these are overkill for a relatively simple set of web pages such as this site.