Simple PHP MVC Framework Example

Today we will discuss how to create MVC sample application in PHP. Because now days in php everybody creating class based structure of application and main problem in class based the all the things is in same function(such as view, model and action).So with help of MVC we will separate all layer.

We will create simple php content management system based on MVC design pattern. This app will have basic structure and page module. The page module will save data into the mysql database and display data into view.

The MVC stands for Model, View and Controller. You can get more information of MVC introduction from Model, View and Controller in MVC

Checkout other Tutorials,

The file structure of MVC application are below:

Where the folders for:

  • config: This folder contains all app configurations level files(db.php).
  • controllers: This folder will contain controllers files where the action will define for UI.
  • css: All app level css files.
  • img: Contains all app images.
  • include: This folder will contain all third party libs.
  • js: This folder will contain app JavaScript files.
  • view: Folder contains all views/UI files.
  • index.php: The main root file, where the app will start to render.

There are following files will create for this example :

  • index.php: The main root file, where the app will start to render.
  • controllers/BaseController.php: This is main base controller and will extended by all child controllers.
  • controllers/PageController.php: This controller file will contain all action methods for page module.
  • models/Model.php: This is main model class and hold all common methods for models.
  • models/Page.php: This is page model class and contains page modules methods.
  • view/page/index.php: This is html file page details file.
  • view/header.php: This is partial html file for layout header and contains theme header info.

Step 1: First we will create index.php file. The index file will get the action handler from URL.

in the above code, We have created a link Page Details. Once, we will click the User link it will call pageInfo method into the PageController.php controller file.

App Controllers File Structure

This php content management system will have controllers folder. It contains all application controllers files.

Step 2: Let’s create a base controller controllers/BaseController.php file. This file contains all methods which will use in all controllers files. This file keep all common controller methods for your php app.

Step 3: Create our module controller controller/PageController.php and extend base controller.

We have defined switch method that will render method based on passed action parameter. The getPageInfo() method will render page/index.php view file.

App Models File Structure

The php content management system have models layer. It will have all models class and use for db related operations.

Step 4: Creating a base_model class. This class contains all method which is common in all module model class.

We ll extend above base model class and implement insertData(), saveData() method into the extended models/Page.php model class.

Step 5: Lets create our module model file which will extend above base model class.

As you can see above code, I have implemented insertData() and saveData() method.

Call Models Method Into Controller File

We have defined controller handler methods and models methods, let’s integrate and call into controller handler method.

Step 6: Let’s call model method in controller file.

Step 7: Created a new config/db.php file which contains database configuration.

Step 8: Created a new config/config.php file which contains configuration level variable.

Step 9: Created a new includes/common_function.php file. This file contains all
helper methods which is available in all modules.

Step 10: Finally we will create view file view/page/index.php in view folder. This file contains HTML and js related functionality.

Step 9: Finally we will create view file view/page/index.php in view folder. This file contains HTML and js related functionality.

Conclusion:

We have defined high level php content management system using MVC design pattern. I did not not defined basic level thing, I have described high level overview for php cms.

Students, are you in search of reliable help with your PHP assignments and solutions? Please address AssignmentCore and get your PHP homework done by experts.