PHP Minimalist Model View Controller (phpMMVC) is a minimalist, lightweight implementation of MVC for PHP. Unlike most PHP frameworks, phpMMVC does nothing more than provide a controller which facilitates easily implementing extension-less URLs (also called pretty URLs) in your application, leaving you free to implement your models and (optionally, but recommended) views however you like. There is no enforced directory structure and no giant framework-specific library of classes and methods to learn. Simply stated, this framework will not get in the way of you developing your application because it is designed to have a learning curve approaching as close to zero as possible.
Download
- 2009-11-22: Version 1.5 (latest)
- 2009-10-08: Version 1.4
- 2009-03-05: Version 1.3
- 2009-02-01: Version 1.2
- 2009-01-18: Version 1.1
- 2009-01-15: Version 1.0
The download contains both the source code of phpMMVC and the source code for the sample application.
File/directory structure provided
The following file/directory structure is included in the source code by default. You may rearrange the directories whichever way you like. However, if you decide to put your models somewhere other than php/models/, then you must change $_CONTROLLER['MODELS_PATH'] accordingly in controller.php.
- .htaccess - Leverages mod_rewrite to direct all HTTP requests for nonexistent file/folder paths to controller.php to parse as extension-less URLs.
- controller.php - Maps HTTP requested URL to the corresponding PHP model file.
- css/ - Empty directory. This is the default (suggested) location for CSS files.
- images/ - Empty directory. This is the default (suggested) location for image files.
- js/ - Empty directory. This is the default (suggested) location for Javascript files.
- php/ - Default location for all PHP code for your application, except for the controller which must be at the root directory.
- .htaccess - Denies directory listings in this folder.
- lib/ - This is the default (suggested) location for any PHP file that is not a model or a view. (Such as an open source class you need to include in a model.)
- .htaccess - Denies directory listings in this folder.
- models/ - This is the default location for PHP model files (business logic). You can change this location by editing controller.php as described above.
- .htaccess - Denies directory listings in this folder.
- views/ - This is the default (suggested) location for PHP view files (HTML templates).
- .htaccess - Denies directory listings in this folder.
Variables provided by controller.php
The controller.php provides an array called $_CONTROLLER which has the following elements.
- $_CONTROLLER['MODEL'] (only in version 1.2+) - Contains the name of the current model without a .php extension.
- $_CONTROLLER['PATH_ARGS'] - Contains an array of all the path arguments contained within the URL. $_CONTROLLER['PATH_ARGS'][0] is always the name of the model file. Supposing a URL of site.com/model/something, then $_CONTROLLER['PATH_ARGS'][1] would contain the string "something". There are examples of how to use this in the sample application models.
- $_CONTROLLER['BASE_DIR'] - Contains the full path of directories leading up to controller.php. This variable can be useful if your application is in a subdirectory of your domain because since extension-less URLs require all HTML file linking to be absolute paths, this variable can allow you to make an absolute link without needing to hardcode the directory path. There are examples of how to use this in the sample application views.
- $_CONTROLLER['404_FALLBACK_MARKUP'] (only in version 1.1+) - Contains the markup for the 404 page (the file not found page) should you not define a custom file for it. You can change this markup if you want, but it is not recommended. You should define a custom 404 file instead. (See settings below.)
Settings in controller.php
The $_CONTROLLER array also stores controller settings whose defaults can be changed at the top of the controller.php's source.
- $_CONTROLLER['MODELS_PATH'] - Relative path to the directory containing your PHP model files. The default is php/models/.
- $_CONTROLLER['DEFAULT_PAGE'] - Default model file to load if there are no path arguments. The default is index.php.
- $_CONTROLLER['404_PAGE'] - Default model file to load if there is a first path argument (meaning $_CONTROLLER['PATH_ARGS'][0] is not an empty string), but no corresponding model file matches the string. The default is 404.php.
- $_CONTROLLER['ENABLE_EXT'] (only in version 1.2+) - Decides whether or not to let the controller accept URLs in the form of domain.ext/model.php in addition to the extension-less domain.ext/model.
Live demo
You can view a live demo sample application running on phpMMVC on my server here. The source code for the sample application is included with the download above.
Dependencies
The controller.php's corresponding .htaccess file is designed to work solely with Apache Web Server with mod_rewrite enabled.
Changelog
- Version 1.5
- Fixed a bug which caused the last item in the $_CONTROLLER['PATH_ARGS'] array to return an incorrect value if GET variables were appended to the URL.
- Version 1.4
- Fixed a bug which caused $_CONTROLLER['MODEL'] to return an incorrect value if GET variables were appended to the URL.
- Version 1.3
- $_CONTROLLER['BASE_DIR'] will now return an empty string rather than '/' if the base directory is the root directory of the domain.
- Version 1.2
- Fixed a bug which caused the feature which allows the loading of models with explicit .php extensions to not work.
- Added a $_CONTROLLER setting called $_CONTROLLER['ENABLE_EXT'] which decides whether or not to let the controller accept URLs in the form of domain.ext/model.php in addition to the extension-less domain.ext/model.
- Added a $_CONTROLLER variable called $_CONTROLLER['MODEL'] which contains the name of the current model without a .php extension.
- Version 1.1
- Cleaned up redundant markup and abstracted it into a $_CONTROLLER variable called $_CONTROLLER['404_FALLBACK_MARKUP'].
- Revised the code comments for clarity.
- Version 1.0
- Initial version.
Licensing
phpMMVC is licensed under the Creative Commons Attribution 3.0 United States License. You are permitted to redistribute and/or modify this work for commercial or noncommercial purposes provided proper attribution to the original author (me) is present in the redistribution.