opine/route

Service wrapper for FastRoute providing a more slim-like interface

3.0.20 2016-12-13 21:48 UTC

README

Service wrapper for FastRoute providing a more slim-like interface.

Stories in Ready

Build Status

Scrutinizer Quality Score

Code Coverage

Background

FastRoute is an extremely fast PHP routing library.

Opine\Route is a service wrapper that makes it easy to define routes via a YAML file, cache routes and execute them.

Route Sample

route:
    GET:
        /sample:                            controller@sampleOutput
        /api/add:                           controller@sampleOutput
        /api/edit:                          controller@sampleOutput
        /api/list:                          controller@sampleOutput
        /api/upload:                        controller@sampleOutput
        /api/upload/file:                   controller@sampleOutput
        /api/upload/file/{name}:            controller@sampleOutput2
        /api2/add:                          [controller@sampleOutput, {before: controller@beforeFilter, after: controller@afterFilter}]
        /api2/edit:                         [controller@sampleOutput, {before: controller@beforeFilter, after: controller@afterFilter}]
        /api2/list:                         [controller@sampleOutput, {before: controller@beforeFilter, after: controller@afterFilter}]
        /api2/upload:                       [controller@sampleOutput, {before: controller@beforeFilter, after: controller@afterFilter}]
        /api2/upload/file:                  [controller@sampleOutput, {before: controller@beforeFilter, after: controller@afterFilter}]
        /api2/upload/file/{name}:           [controller@sampleOutput2, {before: controller@beforeFilter, after: controller@afterFilter}]
        /sample2:                           [controller@sampleOutput, {name: Sample}]
        /sample3/{name}:                    [controller@sampleOutput2, {name: SampleParam}]
        /sample3/{name}/{age}/{location}:   [controller@sampleOutput3, {name: SampleParamAssoc}]
        /redirect:                          controller@sampleRedirect

Load / Execute Routes

$routeFile = '/var/www/project/config/routes/route.yml';
$containerFile = '/var/www/project/config/containers/container.yml';
$webroot = '/var/www/project/public';
$config = new \Opine\Config\Service($webroot);
$config->cacheSet();
$container = \Opine\Container\Service::instance($webroot, $config, $containerFile);
$routeService = new Opine\Route\Service($webroot, $container);
$routeModel = new Opine\Route\Model($webroot, $routeService);
$routeModel->yaml($routeFile);
$response = $this->route->run('GET', '/sample');
var_dump($response);

Installation

composer require "opine/route:dev-master"
composer install

Author

Ryan Mahoney can be reached at ryan@virtuecenter.com or @vcryan on Twitter.