/simplemvc-framework

Core of the SimpleMVC framework

Primary LanguagePHPMIT LicenseMIT

SimpleMVC

Build status

SimpleMVC is an MVC framework for PHP based on the KISS principle:

"Keep It Simple, Stupid"

The goal of this project is to offer a simple to use and fast framework for PHP applications using PSR standards.

SimpleMVC uses the dependency injection pattern to manage the dependencies between classes and the FastRoute library for implementing the routing system.

SimpleMVC uses the following PSR standards, from the PHP-FIG initiative:

This project was born as educational library for the course PHP Programming by Enrico Zimuel at ITS ICT Piemonte in Italy.

Since than, the project has been evoluted and used also for building web application in production. We decided to create a more general purpose project and this was the beginning of this repository.

Introduction

SimpleMVC implements the Model–View–Controller (MVC) architectural pattern using Dependency injection and PSR standards.

A SimpleMVC application looks as follows:

chdir(dirname(__DIR__));
require 'vendor/autoload.php';

use DI\ContainerBuilder;
use SimpleMVC\App;
use SimpleMVC\Emitter\SapiEmitter;

$builder = new ContainerBuilder();
$builder->addDefinitions('config/container.php');
$container = $builder->build();

$app = new App($container);
$app->bootstrap(); // optional
$response = $app->dispatch(); // PSR-7 response

SapiEmitter::emit($response);

In this example we use a DI container with PHP-DI. We create a SimpleMVC\App object using the previous container.

The application can be configured using the config/container.php file. An example is as follows:

// config/container.php
use App\Controller;

return [
    'config' => [
        'routing' => [
            'routes' => [
                [ 'GET', '/', Controller\HomePage::class ]
            ]
        ]
    ]
];

The steps to manage the request are:

  • bootstrap() (optional), here you can specify any bootstrap requirements;
  • dispatch(), where the HTTP request is dispatched, executing the controller specified in the route.

The dispatch() returns a PSR-7 response. Finally, we can render the response using an emitter. In this example we used a SapiEmitter to render the PSR-7 response in the standard output.

The previous PHP script is basically a front controller of an MVC application (see diagram below).

MVC diagram

In this diagram the front controller is stored in a public/index.php file. The public folder is usually the document root of a web server.

Using a pipeline of controllers

If you want you can specify a pipeline of controllers to be executed for a specific route. For instance, imagine to have a route as follows:

// config/container.php
use App\Controller;
use SimpleMVC\Controller\BasicAuth;

return [
    'config' => [
        'routing' => [
            'routes' => [
                [ 'GET', '/admin', [BasicAuth::class, Controller\HomePage::class ]
            ]
        ],
        'authentication' => [
            'username' => 'admin',
            'password' => '1234567890'
        ]
    ]
];

The route GET /admin will execute the BasicAuth controller first and, if the authentication will be successfull, the HomePage controller after.

This is a pipeline of two controllers executed in order. The BasicAuth is a simple implementation of the Basic Access Authentication. This controller uses the username and password configuration in the authentication section.

If the authentication is not success, the BasicAuth emits an HaltResponse that will stop the pipeline execution. HaltResponse is a special PSR-7 that informs the SimpleMVC framework to halt the execution.

Passing attributes between controllers

If you need to pass an attribute (parameter) from a controller to another in a pipeline of execution you can use the AttributeInterface. For instance, imagine to pass a foo attribute from a controller A to controller B, using the follwing routing pipeline:

// config/container.php
use App\Controller;

return [
    'config' => [
        'routing' => [
            'routes' => [
                [ 'GET', '/', [Controller\A::class, Controller\B::class ]
            ]
        ]
    ]
];

You need to create the controller A as follows:

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use SimpleMVC\Controller\AttributeInterface;
use SimpleMVC\Controller\AttributeTrait;
use SimpleMVC\Controller\ControllerInterface;

class A implements ControllerInterface, AttributeInterface
{
    use AttributeTrait;

    public function execute(
        ServerRequestInterface $request, 
        ResponseInterface $response
    ): ResponseInterface
    {
        $this->addRequestAttribute('foo', 'bar');
        return $response;
    }
}

We can use an AttributeTrait that implements the AttributeInterface with the addRequestAttribute(string $name, $value). This function adds a PSR-7 attribute into the $request for the next controller.

In order to get the foo parameter in the B controller you can use the PSR-7 standard function getAttribute() from the HTTP request, as follows:

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use SimpleMVC\Controller\ControllerInterface;

class B implements ControllerInterface
{
    public function execute(
        ServerRequestInterface $request, 
        ResponseInterface $response
    ): ResponseInterface
    {
        $attribute = $request->getAttribute('foo');
        pritnf("Attribute is: %s", $attribute);
        return $response;
    }
}

Notice that you don't need to implement the AttributeInterface for the B controller since we only need to read from the $request.

Quickstart

You can start using the framework with the skeleton application.

Copyright

The author of this software is Enrico Zimuel and other contributors.

This software is released under the MIT License.