Simple PHP MVC Framework Example

Today we will discuss how to create an MVC sample application in PHP. Because nowadays in PHP everybody creates a class-based structure of the application and the main problem in class-based that all the things are in the same function(such as view, model and action). So with the help of MVC, we will separate all layers.

We will create a simple PHP content management system based on the MVC design pattern. This app will have a basic structure and page module. The page module will save data to the MySQL database and display it in view.

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

Checkout other Tutorials,

The file structure of the MVC application is below:

Where the folders for:

  • config: This folder contains all app configurations level files(db.php).
  • controllers: This folder will contain controller 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: The Folder contains all views/UI files.
  • index.php: The main root file, where the app will start to render.

The following files will be created for this example :

  • index.php: The main root file, where the app will start to render.
  • controllers/BaseController.php: This is the main base controller and will be extended by all child controllers.
  • controllers/PageController.php: This controller file will contain all action methods for the page module.
  • models/Model.php: This is the main model class and holds all common methods for models.
  • models/Page.php: This is a page model class and contains page module methods.
  • view/page/index.php: This is HTML file page details file.
  • view/header.php: This is a partial HTML file for the 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.

<div id="container">
<ul>
<li><a class="menu" href="PageController.php?action=pageInfo">Page Details</a></li>
</ul>
</div>

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.

abstract Class BaseController {
	public function render($file) {
	include '../' . $file .'.php';
   }
}

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

$obj = new Controller();

switch($_REQUEST['action']) {
	case 'pageInfo' :
		$obj->getPageInfo();
	break;
	}
Class PageController Extends BaseController {

	public function getPageInfo()
	{
		$this->render('page/index');
	}
}

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.

abstract Class Model {
	protected function insertData($table) {
        }
        protected function saveData($table) {
        }
}

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.

Class Page Extends Model {

	public function saveData()
	{
		try {
			//write your logic
			}
		} catch (Exception $e) {
			return $e-&gt;getMessage();
		}
	}
	protected function insertData() {
		try {
			//write your logic
			}
		} catch (Exception $e) {
			return $e-&gt;getMessage();
		}
	}
}

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 controllers/BaseController.php.

public function saveData()
{
   $result = $this->model->saveData();
}

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

dbhost, $this->username, $this->password, $this->dbname) or die("Connection failed: " . mysqli_connect_error());

		/* check connection */
		if (mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		} else {
			$this->conn = $con;
		}
		return $this->conn;
	}
}
?>

Step 8: Created a new config/config.php file that 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 a view file view/header.php in the view folder. This file contains HTML and js related functionality.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

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

<div class="container">
<div class="row">
<div class="col-md-12">
<h1 class="text-center">Hello! Page Details page</h1>
</div>
</div>
</div>

Conclusion:

We have defined high-level PHP content management system using an MVC design pattern. I did not define the basic level things, I have described a high-level overview for PHP cms.

5 thoughts on “Simple PHP MVC Framework Example

Leave a Reply

Your email address will not be published. Required fields are marked *