MVC - Model view controller
Model view controller(mvc) is the design pattern nowadays used in the production.
Model - It contains the code to manage the databases. For example you can see sql requests like INSERT INTO tasks(id,title,description,creation_date,edition_date)
values('55','"$_POST['title']"','"$_POST['task']"','"$_POST['date']"','"$_POST['edit_date']"')").
View - From the name itself it implies that it is the view part normally contains HTML codes. Sometimes it has PHP variables and conditions. For example if we want to display a post of a user.
Controller - Controller has all the decision making codes or else logical parts goes here. It is the intermediate between model and view. For instance : If an user requests for an page the controller invokes the method from the model and gives back it to the view.
For beginners I have created a directory structure of mvc which you can either git clone or download the zip file and you can extract into the directory where you are going to create your mvc application
To clone the structure use the git command :
git clone https://github.com/ASHOK6266/myOwnStructure_mvc.git
I have also added database file to the structure which is a Singleton pattern
Once you clone the structure you will see the database file in Models/Application/db.php
Database connection is the Singleton pattern which restricts the class instantiation again and again.
Moreover the singleton pattern makes your application faster.
Don't forget to add your dbname,username,password in the db.php file
STEP 0:
I created database named myOwnStructure_mvc and todolist table with id,title and description.
STEP 1:
Models/Application/App.php
I created class Tasks and method get_task();
The method get_task() will retrieve all the posts from the database.
STEP 2:
Controllers/Application/AppController.php
I created class AppController and method show_tasks ;
The method show_tasks will invoke the model (i.e method get_task) from the Models/Application/App.php
Don't forget to include model file here include('../../Models/Application/App.php');
STEP 3:
Views/Application/App.php
I just called the controller methods here i.e show tasks.
Normally no logic and security should be in views.
I have also commented inside each file . You can learn from it. I have just given basic documentation. You can start building the application todolist.
If you wish to build application other than todo list. You can just replace the codes.
ADDITIONAL INFORMATION
Yes!!!!! We are done with the basics but there are lot to learn.
The Index file in the project is the entry point of the application. To make it as entry point you have configure your server.
.htaccess is the configuration file for web server running on web applications.
Top comments (1)
The sql code given in the tutorial is used for understanding purposes. Make sure to handle the sql requests in a secure way.