meekframework / route
This package is abandoned and no longer maintained.
The author suggests using the meraki/route package instead.
A simple framework for routing URLs in PHP.
v0.4.0
2017-10-23 05:28 UTC
Requires
- php: ^7.1
- psr/http-message: ^1.0
Requires (Dev)
- phpdocumentor/phpdocumentor: ^2.9
- phpmd/phpmd: ^2.4
- phpunit/phpunit: ^6.3
- scrutinizer/ocular: ^1.3
- squizlabs/php_codesniffer: ^2.6
- zendframework/zend-diactoros: ^1.3
This package is not auto-updated.
Last update: 2020-11-22 11:57:10 UTC
README
A simple framework for routing.
Installation
With Composer:
composer require meekframework/route
Usage
Using the router with the front controller pattern is probably the common use:
use Meek\Route\Collection; use Meek\Route\Mapper; use Meek\Route\Matcher; use Meek\Route\Dispatcher; use Zend\Diactoros\Response\TextResponse; use Zend\Diactoros\ServerRequestFactory; use Zend\Diactoros\Response\SapiEmitter; use Meek\Route\TargetNotMatched; use Meek\Route\MethodNotMatched; use Psr\Http\Message\ServerRequestInterface; // any PSR7 compliant library can be use to generate server requests $serverRequest = ServerRequestFactory::fromGlobals(); $collection = new Collection(); $map = new Mapper($collection); $matcher = new Matcher($collection); $dispatcher = new Dispatcher($matcher); $map->get('/', function (ServerRequestInterface $request) { // a PSR7 responses must be returned return new TextResponse('Hello, world!'); }); $map->get('/posts', function (ServerRequestInterface $request) { return new TextResponse('Viewing all posts!'); }); $map->get('/posts/:id', function (ServerRequestInterface $request) { return new TextResponse(sprintf('You are viewing post "%s"!', $request->getAttribute('id'))); }); // etc... $map->head('/', function (ServerRequestInterface $request) { ... }); $map->put('/', function (ServerRequestInterface $request) { ... }); $map->post('/', function (ServerRequestInterface $request) { ... }); $map->delete('/', function (ServerRequestInterface $request) { ... }); $map->options('/', function (ServerRequestInterface $request) { ... }); try { $response = $dispatcher->dispatch($serverRequest); } catch (TargetNotMatched $e) { $response = new TextResponse('Not Found', 404); } catch (MethodNotMatched $e) { $response = new TextResponse('Method Not Allowed', 405, ['allow' => $e->getAllowedMethods()]); } (new SapiEmitter())->emit($response);
API
Contributing
See CONTRIBUTING.md.
Credits/Authors
License
The MIT License (MIT). Please see LICENSE.md for more information.