slip/router

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

Enhanced Slim router with namespaces support

dev-master 2019-04-20 19:26 UTC

This package is not auto-updated.

Last update: 2024-04-29 17:59:53 UTC


README

Slip Router allows you to add namespaces to your Slim routes and route groups.

Install

Via Composer

$ composer require slip/router

Usage

You need to replace default Slim router with Slip router:

$app = new \Slim\App();
$container = $app->getContainer();

$container['router'] = function ($container) {
    $routerCacheFile = false;

    if (isset($container->get('settings')['routerCacheFile'])) {
        $routerCacheFile = $container->get('settings')['routerCacheFile'];
    }

    $router = (new \Slip\Routing\Router())->setCacheFile($routerCacheFile);
        
    $router->setContainer($container);

    return $router;
};

Then you can set your routes namespaces

$app->get('/profile/user/{id}', 'UserController:getProfile')->setNamespace('App\Http\Controllers');

Or you can add namespace to the whole route group

$app->group('/profile/user', function ($app) {
    $app->get('/{id}, 'UserController:getProfile');
})->setNamespace('App\Http\Controllers');

Nested route groups also support namespaces

$app->group('/profile', function ($app) {
    $app->group('/user', function ($app) {
        $app->get('/{id}, 'UserController:getProfile');
    })->setNamespace('Http\Controllers');
})->setNamespace('App');

Route namespace appends to route groups namespaces

$app->group('/profile', function ($app) {
    $app->group('/user', function ($app) {
        $app->get('/{id}, 'UserController:getProfile')->setNamespace('Controllers');
    })->setNamespace('Http');
})->setNamespace('App');

Testing

$ php ./vendor/bin/phpunit

License

The MIT License (MIT).