stratifyphp / framework

Middleware-based framework built on PHP-DI and Silly
MIT License
20 stars 1 forks source link

Stratify is a try at writing a middleware-based web framework.

Work in progress

A quick intro:

The documentation is not up to date, and the project is highly likely to change, TODO:

Getting started

composer require stratify/framework

Example of web/index.php:

<?php

use Stratify\ErrorHandlerModule\ErrorHandlerMiddleware;
use Stratify\Framework\Application;
use Zend\Diactoros\Response\HtmlResponse;
use function Stratify\Framework\pipe;
use function Stratify\Framework\router;

require __DIR__ . '/vendor/autoload.php';

// Prepare the HTTP stack
$http = pipe([
    // This is a list of middlewares

    // The first middleware is the error handler, it will
    // catch all errors and display the error page
    ErrorHandlerMiddleware::class,

    // The application's router
    // See https://github.com/stratifyphp/router for more details
    router([
        // Routes
        '/' => function () {
            return new HtmlResponse('Welcome!');
        },
        '/about' => function () {
            return new HtmlResponse('More information about us');
        },
    ]),
]);

// List of packages containing PHP-DI config to include
// See https://github.com/PHP-DI/Kernel for more details
$modules = [
    'stratify/error-handler-module', // error handling
];

// The application environment
$env = 'dev';

$app = new Application($modules, $env, $http);

// Run the HTTP application
$app->http()->run();

Adding PHP-DI configuration

PHP-DI configuration is managed by the PHP-DI Kernel, read its documentation to learn more. Below is a short example.

Set up your application name in composer.json, this will be used by PHP-DI's kernel as your module name:

{
    "name": "app",
    ...
}

You can use any name you want, app is a good default (like Symfony's AppBundle).

Add your module name to the list of PHP-DI modules to load:

$modules = [
    'stratify/error-handler-module',
    'app',
];

Twig

Install the Twig module:

composer require stratify/twig-module

Enable the package module in the PHP-DI module list:

$modules = [
    'stratify/error-handler-module',
    'stratify/twig-module',
    ...
];

You can configure the directory in which views are stored using PHP-DI config (see the section above):

return [
    ...

    'twig.paths' => [
        // Configure the directory using the alias `app`
        // In this example views are stored in the `res/views/` directory
        // You can then use the `@app/...` notation to render or include Twig templates
        'app' => __DIR__ . '/../views',
    ],
];

You can then inject the Twig_Environment class in your services, or in your controllers. For example:

$http = pipe([
    ErrorHandlerMiddleware::class,
    router([
        // Routes
        '/' => function (Twig_Environment $twig) {
            return $twig->render('@app/home.twig');
        },
    ]),
]);