This project aims to provide an MVC PHP framework for you to use in your projects. If you want to see an example there is a complete example here about an e-commerce website.
Briefly, MVC (Model View Controller) is a design pattern widely used in projects because it leaves the project structured in order to facilitate the identification of application modules, understand how it is structured, in addition to facilitating maintenance. It structures the project in three modules:
Name | Funcion |
---|---|
Models | Responsible for business logic |
View | Responsible for the visual part |
Controllers | Responsible for the behavior of the visual part |
This project adopted the following naming pattern:
Type | Name |
---|---|
Controller | <ControllerName>Controller |
View | <viewName> |
In src
folder is all what you need to apply this pattern in your project.
This framework uses autoload for loading classes. Consequently, you have to use namespace's and use's in classes.
All models have to extend Model class. Also, they must use namespace models
. Lastly, when you create your modal classes, put them in models
folder.
The views have all html content, with as little php as possible. To use the information present in a controller, use the data sent by the controller through an array (you don't use the array directly, but instead use a variable whose name is the same as the array key name).
- Controller
- View
Controllers have to extend Controller class. Also, they must use namespace controllers
. All controllers must be in controllers
folder. This class has 2 methods for showing a view and another that will be invoked depending on the url provided.
Also, if you call a view using loadTemplate
function, you must send the following attributes to the view:
- title
- description
- keywords (useful for SEO)
- robots ('index' if the page should be indexed by SEO; 'noindex' otherwise)
When a url is sent to the server, it will parse it to identify three components:
- Controller
- Action
- Parameters
Name | Function |
---|---|
Controller | Will invoke the identified controller |
Action | It is the method of the identified controller that will be called |
Parameters | It is the parameters of the identified action |
This parse is done in Core class. The main controller, that is, the one that will be called when accessing the website url, will be HomeController.
Note: If the sent controller does not exist, will be chosen NotFoundController, which will load 404 view.
When some of these components are not sent they will have the following default values:
Name | Function |
---|---|
Controller | HomeController |
Action | index |
Parameters | Empty array |
All views are loaded inside a template view. You can define your website standards here, and all views will follow this standard. To do that, remember loading views with loadTemplate
method.
The MVC structure is in src
folder.
Name | Type | Function |
---|---|---|
media | Directory |
Visual information of the project |
src | Directory |
MVC framework |
Name | Type | Function |
---|---|---|
assets | Directory |
Contains all application content files |
controllers | Directory |
Contains all application controller classes |
core | Directory |
Contains the classes responsable for the MVC operations |
models | Directory |
Contains all application model classes |
vendor | Directory |
Folder created by Composer - responsable for classes autoload |
views | Directory |
Contains all application view classes |
.htaccess | File |
Responsible for friendly url |
composer.json | File |
File created by Composer |
config.py | File |
Website configuration file (Database and website location) |
environment.php | File |
File responsible for defining which environment is in use |
index.php | File |
File responsible for starting the website |
robots.txt | File |
Determines which files crawlers can see |
Indicates which environment is in use. There are two:
- Development
Loads configuration for local server.
- Production
Loads configuration for online server.