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,
Where the folders for:
db.php
).The following files will be created for this example :
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.
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.
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->getMessage(); } } protected function insertData() { try { //write your logic } } catch (Exception $e) { return $e->getMessage(); } } }
As you can see above code, I have implemented insertData()
and saveData()
method.
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>
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.
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More
View Comments
Helpful..........
thank you for your suggestion
Thanks for your feedback, Sure i ll try :)
Hi
you say into the controler file. but which exactly?
there are two controller files
I have added, thanks