species/app

There is no license information available for the latest version (0.3.3) of this package.

Yet another Simple Application Framework.

0.3.3 2019-04-28 18:26 UTC

This package is auto-updated.

Last update: 2024-04-29 03:56:57 UTC


README

Yet another Simple Application Framework.

Glue for Slim and Twig, configured with PHP-DI, written in php 7.2!

Installation

There is a skeleton available if you want to start a new project:

composer create-project species/app-skeleton my-project-path

Or use it as a library:

composer require species/app

Middleware

Example how to add middleware with PHP-DI config:

<?php 

use function DI\add;
use Species\App\Middleware;

return [
    'settings.middleware' => add([
        Middleware\TwigDebugMiddleware::class,
        Middleware\CsrfValidationMiddleware::class,
        Middleware\AddRouteNameToTwigMiddleware::class,
        // ...
    ]),
];

AddRouteNameToTwigMiddleware

Will add routeName as a twig global variable, if it can find one.

CsrfValidationMiddleware

Will test POST requests if the csrfToken field is valid. When invalid, the session will be cleared, the session ID will be regenerated and an InvalidCsrfToken exception will be thrown.

TwigDebugMiddleware

Will add the twig debug extension when twig debug is enabled.

Slim HTTP cache

The slim/http-cache library is also packaged.

Example how to config this middleware:

<?php 

use function DI\add;
use Slim\HttpCache\Cache as HttpCacheMiddleWare;

return [
    // Default settings
    'settings.httpCache.type' => 'private',
    'settings.httpCache.maxAge' => 86400,
    'settings.httpCache.mustRevalidate' => false,

    // Add middleware  
    'settings.middleware' => add([
        HttpCacheMiddleWare::class,
    ]),
];

Twig extensions

Example how to add twig globals and extensions with config:

<?php 

use function DI\add;
use Species\App\TwigExtension;

return [
    // Twig globals
    'settings.twig.globals' => add([
        'foo' => 'bar',
        // ...
    ]),
    
    // Twig extensions
    'settings.twig.extensions' => add([
        TwigExtension\CsrfTwigExtension::class,
        TwigExtension\ReflectionTwigExtension::class,
        TwigExtension\RouterTwigExtension::class,
        // ...
    ]),
];

CsrfTwigExtension

Adds the function csrfTokenInput() to render the hidden input field. Or use csrfToken() which returns only the token.

ReflectionTwigExtension

Adds the following functions:

  • fqcn(object $object): string Returns the fully qualified class name of given object.
  • className(object $object): string Returns the class name of given object without namespace.
  • instanceOf(object $object, string $class): bool Tests if given object is an instance of given class.

RouterTwigExtension

Adds the global baseUrl and following functions:

  • pathFor(string $name, array $data = [], array $queryParams = []): string Returns the path for given route.
  • urlFor(string $name, array $data = [], array $queryParams = []): string Returns the full url for given route.