There is something about MVC in PHP

3A-STI Students must read !

I will describe all steps required to develop the most tiny MVC pattern ever in PHP:

M:stands for (data) Model
V:stands for (data) View
C:stands for Controller
No alt text provided for this image
Logical schema

Model: The Model encloses the application data.

View: The View element is used for presenting the data of the model to the user. It interacts with the user and presents the result formed by the model

Controller: The entrypoint into the application. It handles the user requests and listens to all the events triggered in the view and prepares the response back. When user request is sent to the controller, it forwards it to the Model to provide some logic on it.

Several important web frameworks have adopted the pattern MVC.

As I said, my goal is to devlop a very tiny MVC from scratch. I hope this guide helps you to find your way through to advanced implementation!

The Model

<?php

class Model {
  private $data=null;
  public function __construct() {
    $this->data="bonjour";
  }
  public function getData():string {
    return $this->data;
  }

  public function setData(string $data) {
    $this->data = $data;
  }
}

?>

This simple class is built around a private data member. The Model coud read and set the data. Very easy to grasp.

The Controller

<?php

class Controller {
  private $model;

  
  public function __construct(Model $model) {
    $this->model=$model;
  }

  public function update(){
    $this->model->setData("Hello");
  }
}

?>

The controller is equiped with two methods:

  • the constructor which permits to link the Controller with the Model
  • the update method to change the member data value

The View

<?php

class View {
 private $model;
 public function __construct(Model $model) {
    $this->model=$model;
 }
  public function out():string {
   return 
     '<a href="index.php?action=update">'.$this->model->getData().'</a>';
  }
}

?>

The View is equiped with two methods:

  • the constructor which permits to link the View with the Model
  • the out method to get and display the member data value from the Model

The final page is the Home page (index.php):

<?php


//INCLUDES
require __DIR__.'/model.php';
require __DIR__.'/view.php';
require __DIR__.'/controller.php';

//INSTANCIATIONS
$model = new Model();
$view = new View($model);
$controller = new Controller($model);

if(isset($_GET['action'])){
  $controller->{$_GET['action']}();
}

echo '<h1>'.$view->out().'</h1>';
?>

Scenario


No alt text provided for this image
When you hover the mouse pointer over "Bonjour", you will see the following URL:
http://localhost/index.php?action=update

When you click on it, you are asking for an update (action=update). The Model will be updated conformly! accordingly, the view displays the updated data in the browser.

voilà , you get it:

No alt text provided for this image

Any question? let me know!

To view or add a comment, sign in

More articles by A. ADELL

  • Produit et propriétés universelles

    note: article en cours ① Mise en jambe Soient A , B, P et C des ensembles ayant les extensions suivantes: A= { John…

  • La correction du gamma

    «La correction du gamma permet de définir la quantité relative des composantes de couleur d’une image. Cette fonction…

  • Sections PLT, GOT, compilation statique vs dynamique

    De nombreux articles sur Internet expliquent les notions de compilation statique et dynamique et le rôle de PLT et GOT…

  • Re-entrancy attack sur "The DAO" & smart contract

    Voilà une attaque malicieuse qui date de 2016 sur les smart contracts! L'initiation des smart contracts sur la…

  • SVD, Data Science, et algèbre linéaire

    SVD = Singular Value Decomposition (Décomposition en valeurs singulières) Mise en jambes Soit 𝔼 un K-espace vectoriel…

  • un peu de (mécanique) quantique (2/10)

    Je considère un système à un seul qubit 1️⃣ vecteur d'état pour les états purs La base considérée est {|uᵢ⟩}, i =1,..

  • Pourquoi "positiver"

    Désolé ce n'est pas un article sur les astuces pour rester positif! 1️⃣ Rester "positif" ! Je considère l'opération de…

  • un peu de (mécanique) quantique

    Système à un seul qubit Un qubit correspond à l'état d'une particule qui peut osciller entre deux états de base: par…

  • Entropie en thermodynamique vs Entropie en théorie de l'information

    Introduction Emmy Noether,mathématicienne allemande, a démontré le théorème suivant : Parce que les lois physiques…

  • md5

    I- Empreinte Les fichiers informatiques ont leurs empreintes aussi!. Le calcul de l'empreinte d'un document emploie une…

Others also viewed

Explore content categories