pMVC

pMVC article image

Update: pMVC source code now hosted on GitHub.

pMVC is not a framework nor a class library. It´s a simple MVC model for the PHP plattform.

pMVC is a simple MVC model with structure and separate of concerns in mind. It does not contains all the fancy full-fledged framework stuffs like Zend Framework, Code Igniter or Symfony. But it do contains models, views, controllers, helpers, class autoloading and controller-action-routing.

pMVC is running on PHP 5.6.31 and PHP 7 and above, but future versions will be made for PHP 7.x only. You´ll find the documentation in the source. I´ve tried to comment the code as much as possible.

History

The story behind pMVC isn´t that complicated. It was created to structure my own PHP based applications. The first embryo was used in my thesis at the university. After that it slowly evolved, one piece at a time, in various of private projects.

At work, I work exclusively with Microsoft technology. Therefore it becomes not so much programming in PHP anymore. There´s simply not enough time. Instead of letting projects languish over time, it feels better to share them with others. pMVC is one of those.

Getting Started

Download from GitHub

pMVC comes in two packages. One minimal containing core files (+ a simple view) only. One containing a simple demo site made with pMVC and the Bootstrap framework.

Core project files Demo site

Source Code

These are the files included, how they are structured in the application and a short description of what they do.

pMVC/
├── Controllers/
├── lib/
│   ├── pMVC/
│       ├── Application.php
│       ├── Controller.php
│       ├── Database.php
│       ├── Html.php
│       ├── IRoute.php
│       ├── Model.php
│       ├── Route.php
│       ├── Url.php
│       ├── View.php
│   ├── .htaccess
├── Models/
├── Views/
├── .htaccess (if running on Apache etc.)
├── config.php
├── index.php
|── web.config (if running on IIS)

 

Controllers/
This is where you put the controller classes.

lib/pMVC/
The pMVC application class library.

lib/pMVC/Application.php
This is where the application starts and library, controllers and models are autoloaded.

lib/pMVC/Controller.php
This is the abstract base class for all controllers. It connects the view and the model.

lib/pMVC/Database.php
This class extends the PDO component and setups a database object. Implemented as a singleton and used by the model base class.

lib/pMVC/Html.php
Represents support for rendering HTML controls in a view.

lib/pMVC/IRoute.php
If you´re writing custom routes you have to implement the IRoute interface.

lib/pMVC/Model.php
This is the abstract base class for all models. It provides a small set of functions for data access that will make your access calls chainable.

lib/pMVC/Route.php
The route contains the logic for mapping a request to a specified controller and action. The route values will be extracted from the URL variable ‘load’. You can build custom routes and pass the route object to the application run method. To do so create a class and implement the IRoute interface.

lib/pMVC/Url.php
Contains methods to build URLs within an application.

lib/pMVC/View.php
Is responsible for putting everything together, extract view data and render the final HTML content.

lib/.htaccess
Denies directly access to the lib directory. Delete if running on Microsoft IIS.

Models/
Models goes in this folder.

Views/
Put views into this folder.

.htaccess
The hypertext access file that will do the rewriting stuff, among other things – running Apache etc. Delete if running on Microsoft IIS.

config.php
The main configuration file. Defines constant values like the site template file and database related stuff.

index.php
The main file where the application will be initialized.

web.config
The server configuration file that will replace .htaccess when running pMVC under Microsoft IIS. Delete if running on Apache.

Execution Life Cycle

To explain the execution process of the pMVC application, I´ve summarize the execution of a page in the list below.

Namespaces

pMVC doesn´t use namespaces at the moment – a legacy from the time when namespaces where not available in PHP (arrived in 5.3.0). To be honest I´m not really sure if I like how namespaces are implemented in PHP. But that´s another story. Will return to this in the future.

License

NO COPYRIGHTS. NO LICENSES. DO WHAT YOU LIKE.