05 july 2011
- Added Configurator class: find the path of the application and loads Loader and Config
- Added Loader class: for loading on the fly other classes
- Added Config class and config.ini file: see FrontController for use
- changed some files name
- should update README
- start folder is in config.ini now
A simple MVC framework
You have the following folders:
- web: here resides your public files (images, css, and index.php)
- app: here you can find application files: models, views, controllers (MVC)
How it works?
How you can see, all the requests goes to index.php from web folder. So you can set up your webserver and .htaccess for this. I will continue to describe the framework without this setup.
The first request is to index.php from web folder.
- Here we include all necesary files (of course we can use autoload, but this is a proof of concept example).
- call for an instance of FrontController class
- call the route method of FrontController class
- display the result (body).
FrontController class (located in front.php file) is a singleton.
I will explain a little the constructor method and the route method. The other ones are obvious.
The constructor tries to match the controller name, the action name, and the parameters from the request URL.
In the class you can specify a start folder (START_FOLDER). In our example should be: START_FOLDER = '/testfolder/web/'
If no controller, and/or no action are specified in the request URL it assume is "index" (case a).
In route method we are using Reflection class to determine if indeed the class for the found controller exists
and is has the specified action
Also, the controller must implement the interface IController:
If all these conditions are valid, we can call the method
In the next step, we are going to index controller class (file index.php from controllers).
Here we have the "index" action.
- From the FrontController class we call getParams() method.
- Create a new View object
- Assign parameters to $view object properties
- Call render method of the $view object
- set content with setBody() front controller method.
The View class needs also some explications.
We overloaded the __set method so we can to assign parameters to $view object properties without they exists before.
We also wrote a __get method to return an empty string if you can to access a unexistent property of a view object.
The render method, simply include the specified file using output buffering.
In the view file (index.php from views folder) you can access previously assigned properties with $this->propertyname.
This MVC framework is mainly based on the example from the book "Pro PHP Patterns, Frameworks, Testing and More" by Kevin McArthur.