nsrosenqvist/soma-router

League Route for the SOMA framework

1.0.0 2020-06-09 12:57 UTC

This package is auto-updated.

Last update: 2022-11-09 18:17:22 UTC


README

Installation

composer require nsrosenqvist/soma-router

Usage

Register the service provider, preferably alias NSRosenqvist\Soma\Router\Facades\Router to Router, and then simply register your routes using the Facade. The API is found here.

The system event router.init is a great place to register your routes.

namespace MyApp;

use Soma\ServiceProvider;
use Psr\Container\ContainerInterface;

use Laminas\Diactoros\Response;

class RouterProvider extends ServiceProvider
{
    public function boot(ContainerInterface $c)
    {
        listen('router.init', function($router) use ($c) {
            // map a route
            $router->map('GET', '/', function (ServerRequestInterface $request) : ResponseInterface {
                $response = new Response;
                $response->getBody()->write('<h1>Hello, World!</h1>');
                return $response;
            });

            // or include a file with all route definitions
            include "path/to/routes.php";
        });
    }
}

License

MIT