Skip to content
Sakya edited this page Jan 23, 2021 · 5 revisions

Slim4 Application

Slim4 framework application wrapper with basic functionality and configurable using array

Component:

  • Application
    Application bootstrap. Can handle both http and console
  • Middleware
    • Session Middleware
      A Symfony base session manager using symfony/session
    • Csrf Middleware
      A Csrf middleware using symfony/session
    • AssetManager Middleware
      An asset manager to load asset outside public folder, cache using symfony/cache
    • Guard Middleware
      Basic Authorization library guarding route with action and HTTP method. Require to implement your own Identity Provider Class and Role Provider Class
  • View
    A View class complete with PhpRenderer and view helper. The class will be injected automatically to all Action during application bootstrap.
  • Console
    Console application using symfony/console

All middleware are optionals

Application

Project Structure

project
    - bin
        - project
        - project.php
    - asset
    - config
        - action.config.php
        - middleware.config.php
        - route.config.php
        - service.config.php
    - public
        - .htaccess
        - index.php
    - src
        - Action
            - Factory
        - Service
            - Factory
    - view
        - layout
            - layout.phtml

Basic Usage

<?php
# Put in public/index.php

require_once __DIR__."/../vendor/autoload.php";

$app = Itseasy\Application();
$app->setConfigPath([
    __DIR__.'/config/*.config.php'
]);
$app->run();
?>

Routing

Routing are define in route.config.php using array, the layout are similar to zend configuration. Example

General Route format

<?php
return [
    "routes" => [
        "routename" => [  // Route name will be set using setName, the name its optional
            "route" => "/", // Slim Route format
            "method" => ["GET"], // HTTP Method default GET if not specify.
            "options" => [
                "action" => Action\Dashboard\Dashboard::action, // Action Class
                "arguments" => [], // Additional arguments, can be retrieve in middleware using getArgument
                "middleware" => "" // Glue single middleware to this route
            ],
            "child_routes" => [
                // Array of route ( same format )
            ]
        ],
    ]
];

Redirect Route format

<?php
return [
    "routes" => [
        "routename"  => [
            "route" => "/test",
            "options" => [
                "redirect" => "/redirect/full/route" // Redirect this route to another, must use full route
            ],
            "child_routes" => [
                // Redirect also support child route
            ]
        ],
    ]
];

Both General and Redirect Route format can have both child_routes format

All action need to be specify in the action factories before using it in the route.config.php.
All route configuration will be register to slim application using map function.
A child_routes will be register using group function to slim application.

For further usage check https://www.slimframework.com/docs/v4/objects/routing.html

Middleware

Middleware are define in the middelware.config.php file. Note that the order is very important. Example

<?php
return [
    "middleware" => [
        "middleware" => [
            Itseasy\Csrf\CsrfMiddleware::class,
            Itseasy\Guard\RouteGuardMiddleware::class,
            Itseasy\Session\SessionMiddleware::class,
            Slim\Middleware\RoutingMiddleware::class,
            Itseasy\Asset\AssetMiddleware::class,
            Slim\Middleware\ErrorMiddleware::class,
        ],
    ]
];
?>

Special Builtin Middleware

  • built in Itseasy\Session\SessionMiddleware must be put before Slim\Middleware\RoutingMiddleware
  • built in Itseasy\Guard\RouteGuardMiddleware must be put before Itseasy\Session\SessionMiddleware
  • built in Itseasy\Csrf\CsrfMiddleware must be put before Itseasy\Session\SessionMiddleware

All custom middleware need to be specify first in the service factories before using it here

Middleware are run using LIFO from Request to Action then FIFO from Action to Response.
To skip middleware execution you can utilize throw and catch a custom exception.

Dependency Injection

Slim App is created with PHP-DI.
PHP-DI will register all factory definition from this array

Array
$config["service"]["factories"]
$config["console"]["command"]["factories"]
$config["view_helper"]["factories"]
$config["action"]["factories"]

Config are register inside PHP-DI by default, and can be retrieve using below code in factory class.
Currently doesn't support custom config class. Will be updated in the future.

<?php
$config = $container->get("Config")->getConfig();
?>

Console Application

Console is run using symfony console.
The Application class will read console configuration and setup the command.

<?php
# Put in bin/<script_name>.php

require_once __DIR__."/../vendor/autoload.php";

$app = Itseasy\Application();
$app->setApplicationType(Itseasy\Application::APP_CONSOLE); // Mark application as console
$app->setConfigPath([
    __DIR__.'/config/*.config.php'
]);
$app->run();
?>

Session

Session can be setup using a configuration file e.g session.config.php, Example

<?php
return [
    "session" => [
        'class' => Session::class,
        'options' => [
            'name' => 'App',
            'cache_expire' => 0,
            'cookie_samesite' => 'strict'
        ],
        'csrf_field_name' => '_csrf',
        'csrf_token_id' => 'app'
    ]
];
?>

The class need to be specify in the service factories before using it here.
Session options is using symfony/session options, you can check the available options there.

AssetManager Middleware

Configuration

<?php
return [
    "asset" => [
        "resolver_configs" => [
            "paths" => [
            ]
        ],
        "caching" => [
            "class" => ""
        ]
    ],
];
?>

Guard Middleware

Configuration

<?php
return [
    "guard" => [
        "identity_provider" => "", // Identity Provider class
        "login_route" => "", // Login route
        "authorization" => [
            "default_role" => "guest", // Default role name
            "whitelist" => [], // Whitelist role
            "role_provider" => "", // Role Provider class
            "roles" => [] // Roles Permission list
        ],
    ]   
];
?>

View

Configuration

<?php

return [
    "view" => [
        "class" => View\View::class, // You can use your custom View class here
        "renderer" => View\Renderer\PhpRenderer::class, // You can use your custom renderer class here
        "default_layout" => "layout/layout",
        "error_layout" => "layout/error",
        "default_template_suffix" => "phtml",
        "template_path" => __DIR__."/../view"
    ],
    "view_helpers" => [
        "aliases" => [
            "url" => View\Helper\UrlHelper::class,
            "flash" => View\Helper\FlashMessageHelper::class,
            "csrf" => View\Helper\CsrfTokenHelper::class,
        ],
        "factories" => [
            View\Helper\FlashMessageHelper::class => View\Helper\Factory\FlashMessageHelperFactory::class,
            View\Helper\UrlHelper::class => View\Helper\Factory\UrlHelperFactory::class,
            View\Helper\CsrfTokenHelper::class => View\Helper\Factory\CsrfTokenHelperFactory::class
        ]
    ],
];
?>

Console

Configuration

<?php
return [
    "console" => [
        "commands" => [],
        "factories" => []
    ]
];
?>