Building MVC applications with loosely coupled components in PHP

This series explains how to create an MVC application based solely on loosely coupled components.

This series supposes that the reader is already familiar with MVC applications, Composer, and loose coupling design.

Setting up the project

The application will be called Xyzzy, and all our project files will reside in a xyzzy directory.

First, let's create the file that contains minimum init settings. We want to display all eventual errors while we develop on our application, and set the encoding to UTF-8:


    <!-- xyzzy/init.php -->

    <?php
        ini_set
("display_errors"1);
        
error_reporting(E_ALL);
        
header("content-type: text/html; charset=utf-8");
    
?>

Next, let's create a basic PHP script that gets the GET query string "name" from the URL and outputs it:


    <!-- xyzzy/hello.php -->

    <?php require_once "init.php"?>

    hello, <?= isset($_GET["name"]) ? $_GET["name"] : "world" ?>

When requesting for the URL http://localhost/xyzzy/hello.php?name=Mathieu, the text hello, Mathieu is displayed on the page.
If the name parameter is omitted (http://localhost/xyzzy/hello.php), the text hello, world will be displayed instead.

Comments