MVC Architecture

Today I will talk about what is MVC architecture with some PHP sample codes.

MVC means model, view, controller:

Model: refers to the data structure.
View: refers to the user interface (UI), with what the user sees and interacts.
Controller: is an “intermediate processor” located between the model and the view.
How it works: the user interacts with the view (UI) to enter some inputs. The controller processes the user’s request, works with the data model. Finally, the controller will update the view to show the results.

Model

It is the representation of information or data that you want to display to user. Model have classes (If you have heard or used Object oriented programming you might have heard about Class). Model gets all these data from database and data can be read, updated and deleted.

View

It is all the front end code via which the website is presented in front of the user (UI/UX). It includes all the CSS, HTML and JavaScript. Also Ajax calls are included. Using view, the user interacts with the website.

Controller

It contains all the logic of your website. Controller also has class with the all methods and application logic. It does as the interface between the Model and View. Most of the application codes are written in this part.

Check out the PHP sample codes below.

<?php

//Model Part
class Model 
{
    public $text;
    public function __construct() 
    {
        $this->text = 'Hello world!';
    }
}

//View Part
class View 
{
    private $model;
    public function __construct(Model $model) 
    {
        $this->model = $model;
    }

    public function output() 
    {
        return '<h1>' . $this->model->text .'</h1>';
    }
}

//Controller Part
class Controller 
{
    private $model;
    public function __construct(Model $model) 
    {
        $this->model = $model;
    }
}


//initiate the model class
$model = new Model();
//controller and the view share the model
$controller = new Controller($model);

$view = new View($model);

echo $view->output();

These are some popular PHP MVC frameworks.

  1. – Laravel
  2. – Symfony
  3. – Yii 2
  4. – CakePHP
  5. – CodeIgniter
  6. – Zend Framework
  7. – Phalcon
  8. – Slim Framework
  9. – FuelPHP

By Yuuma.



アプリ関連ニュース

お問い合わせはこちら

お問い合わせ・ご相談はお電話、またはお問い合わせフォームよりお受け付けいたしております。

tel. 06-6454-8833(平日 10:00~17:00)

お問い合わせフォーム