This is a simple MVC project template based on OOP and written in PHP. It also contains Database class which helps connecting to mySQL database.
I decided to write it because I needed to have fully working MVC template without using any framework.
With finishing this project everyone who doesn't want to use frameworks will not waste time in creating from scratch the MVC template.
index.html file is opened by default. All Important files and scripts are loaded.
It is created an instance of a class Application which processes the URLs, checks if controller or view exists and executes them.
If there are no controller and view set in the URL then the default controller and view are set.
If you request controller or action that not exist then the Page Not Found view is returned.
Every custom defined controller extends the base Controller class.
In the base Controller class the view and model are set.
View class implements the View logic and it is responsible for rendering the view. And the model will initialize and map your data whether you read it from file or database.
Let's say we have homeController and index View.
Firstly we need to create homeController class in the Controller folder. homeController will extend the Controller class in order to inherit its properties and functions.
Secondly we need to create our view index.phtml file in the view folder. Put your HTML code here.
Then we need to write index function in homeController class, which will load the index.phtml file from view folder. We need to initialize the view name and pass parameters with data if there are. If there are no parameters to pass, just pass empty array.
If data needs to be read, then you need to create a model where you need to create function to initialize instance of aDatabase class with your credentials and write your logic to execute your query or stored procedure. You can find out how in the carController.
Finally we need to call the render function. If the index.phtml file exists, then the file is loaded, filled with the data and returned to the client.
The project is separated in two main folders - app and public.
-
The app folder contains all the server logic like Controller class, View class, all models, etc.
-
The public folder contains all the client logic - like base index.html file, js files, css styles, etc.
-
App folder
- config
- This folder contains file with configuration values such as database name, database user, etc.
- controller
- This folder will contain all the custom defined Controllers which will process the methods.
- core
- This folder contains the base Application class which prepares the URL, checks if required controller or view exist and executes them.
- There is also the Base Controller class which implements the Controller logic. It sets the View and the Model.
- There is also the Base View class which implements the View logic. It is responsible for rendering the view.
- data
- This folder contains the TXT file with cars data. It is used to show how to read data from file in compare to how to read data from Database.
- There is also carsDB.txt file which contains definition of tables and procedures used for demo. You can execute the script in your phpMyAdmin in order to create the database and tables and procedures. You can set your own credentials for database connection in config.php file.
- Helper
- This folder contains the base Database class which opens the connection to the mySQL database, closes the conection, as well as creates an instance of mysqli.
- model
- This folder will contain all custom defined models from the developer. They are responsible for reading and mapping the data in order to pass it to the view and show it to the client.
- and view folder
- This folder will contain all custom defined views or in other words all html webpages which the client will see.
- config
-
Public folder
- index.html
- This is the init page of the Web Application. This is the first page to load. Application instance is created here in order to start the work of the Application. All required scripts and files are loaded.
- .htaccess
- Website configuration.
- style.css
- All common styles.
- js folder
- This folder will contain all JavaScript files.
- index.html