phprtc / http-router
PHPRTC Http Router
Requires
- php: >=8.1
- nikic/fast-route: ^1.3
- opis/closure: ^3.6
- phprtc/contracts: dev-master
Requires (Dev)
- phpstan/phpstan: ^1.4
- phpunit/phpunit: ^9.5
- symfony/var-dumper: ^6.0
This package is auto-updated.
Last update: 2024-10-24 21:15:17 UTC
README
An elegant http router built on top of FastRoute to provide more easy of use.
Upgrade Guide
Check ChangeLog file
Installation
composer require phprtc/http-router
Usage
Simple example
use RTC\Http\Router\Route; use RTC\Http\Router\Dispatcher; require('vendor/autoload.php'); Route::get('/', function () { echo 'Hello world'; }); $method = $_SERVER['REQUEST_METHOD']; $path = $_SERVER['REQUEST_URI']; //create route dispatcher $dispatcher = Dispatcher::collectRoutes() ->dispatch($method, $path); //determine dispatch result switch (true) { case $dispatcher->isFound(): $controller = $dispatcher->getRoute()->getController(); $controller($dispatcher->getUrlParameters()); break; case $dispatcher->isNotFound(): echo "Page not found"; break; case $dispatcher->isMethodNotAllowed(): echo "Request method not allowed"; break; }
Controller-like example
use RTC\Http\Router\Route; Route::get('/home', 'MainController@home');
Advance usage
use RTC\Http\Router\Route; Route::prefix('user')->name('user.') ->namespace('User') ->middleware('UserMiddleware') ->group(function (){ Route::get('profile', 'UserController@profile'); Route::put('update', 'UserController@update'); });
More Advance Usage
use RTC\Http\Router\Route; Route::prefix('user') ->prepend('api') ->append('{token}') ->middleware('UserMiddleware') ->group(function (){ Route::get('profile', 'UserController@profile'); Route::put('update', 'UserController@update'); }); // => /api/user/{token}
Defining route param types
use RTC\Http\Router\Route; // id => must be number Route::get('users/{id}', 'Controller@index')->whereNumber('id'); // name => must be alphabetic Route::get('users/{name}', 'Controller@profile')->whereAlpha('name'); // username => must be alphanumeric Route::get('users/{username}', 'Controller@profile')->whereAlphaNumeric('username'); // Manually provide regular expression pattern to match parameter with Route::get('/users/{id}', 'a')->where('id', '[0-9]+'); Route::get('/users/{user}/posts/{post}', 'Ctrl@method')->where([ 'user' => '[a-zA-Z]+', 'post' => '[0-9]+' ]);
Route Fields
Fields help to add more description to route or group of routes
use RTC\Http\Router\Route; Route::prefix('user') ->middleware('User') ->addField('specie', 'human') ->group(function (){ Route::get('type', 'admin')->addField('permissions', 'all'); Route::get('g', fn() => print('Hello world')); });
Route::match()
use RTC\Http\Router\Route; use RTC\Http\Router\Collector; use RTC\Http\Router\Dispatcher; require 'vendor/autoload.php'; $controller = fn() => print time(); Route::match(['get', 'post'], '/user', $controller) ->middleware('auth') ->namespace('App') ->name('home'); $collector = Collector::create()->collect(); $dispatchResult = Dispatcher::create($collector) ->dispatch('get', '/user/hello'); var_export($dispatchResult->getRoute());
Route::match() with named routes
use RTC\Http\Router\Route; Route::match(['get', 'post'], 'login', 'AuthController@login')->name('login.'); //Will generate below routes Route::get('login', 'AuthController@login')->name('login.get'); Route::post('login', 'AuthController@login')->name('login.post');
Route::any()
use RTC\Http\Router\Route; use RTC\Http\Router\Collector; use RTC\Http\Router\Dispatcher; $controller = fn() => print time(); Route::any(['/login', '/admin/login'], 'get', $controller); $collector = Collector::create()->collect(); $dispatchResult1 = Dispatcher::create($collector) ->dispatch('get', '/login'); $dispatchResult2 = Dispatcher::create($collector) ->dispatch('get', '/admin/login');
Route::matchAny()
use RTC\Http\Router\Route; Route::matchAny( ['get', 'post'], ['/customer/login', '/admin/login'], 'MainController@index' ); //Which is equivalent to: Route::get('/customer/login', 'MainController@index'); Route::post('/customer/login', 'MainController@index'); Route::get('/admin/login', 'MainController@index'); Route::post('/admin/login', 'MainController@index');
Route::resource()
use RTC\Http\Router\Route; Route::resource('photos', 'App\Http\Controller\PhotoController');
Code above will produce below routes
Crud::create()
use RTC\Http\Router\Crud; Crud::create('/', 'Controller')->go();
Code above will produce below routes
Why not use Route::resource()?
Crud creator generates 6 routes, one of the routes which deletes all record in the endpoint.
With Crud creator you can choose which routes to create or not.
use RTC\Http\Router\Crud; //Disabling route creation Crud::create('/', 'Controller') ->disableIndexRoute() ->disableStoreRoute() ->disableDestroyAllRoute() ->disableShowRoute() ->disableUpdateRoute() ->disableDestroyRoute() ->go(); //Specifying custom route parameter name Crud::create('/', 'Controller')->parameter('userId'); //Specify parameter type Crud::create('/', 'Controller')->numericParameter(); Crud::create('/', 'Controller')->alphabeticParameter(); Crud::create('/', 'Controller')->alphaNumericParameter();
Routes as configuration
//routes.php use RTC\Http\Router\Route; Route::get('/', 'MainController@index'); Route::get('/help', 'MainController@help'); //server.php use RTC\Http\Router\Collector; $collector = Collector::create() ->collectFile('routes.php') ->register(); $routes = $collector->getCollectedRoutes();
Caching
Cache routes so that they don't have to be collected every time.
use RTC\Http\Router\Collector; $collector = Collector::create() ->collectFile('routes.php') ->cache('path/to/save/cache.php', false) ->register(); $routes = $collector->getCollectedRoutes();
Caching routes with closure
use RTC\Http\Router\Route; use RTC\Http\Router\Collector; Route::get('/', function (){ echo uniqid(); }); $collector = Collector::create() ->collect() ->cache('path/to/save/cache.php', true) ->register(); $routes = $collector->getCollectedRoutes();
Note that you must specify that your routes contains closure
Passing Default Data
You can alternatively pass data to be prepended to all routes.
Cached routes must be cleared manually after setting/updating default route data.
use RTC\Http\Router\Collector; $collector = Collector::create(); $collector->collectFile('api-routes.php', [ 'prefix' => 'api', 'name' => 'api.', 'namespace' => 'Api\\' ]); $collector->register();
Changing Delimiter
For usage outside of web context, a function to change default delimiter which is "/" has been provided.
use RTC\Http\Router\Route; use RTC\Http\Router\Collector; use RTC\Http\Router\Dispatcher; require 'vendor/autoload.php'; Route::prefix('hello') ->group(function () { Route::get('world', fn() => print('Hello World')); }); $collector = Collector::create() ->prefixDelimiter('.') ->collect() ->register(); $dispatchResult = Dispatcher::create($collector) ->dispatch('get', 'hello.world'); var_export($dispatchResult);
Finding route & generating route uri
use RTC\Http\Router\Route; use RTC\Http\Router\Collector; Route::get('/users', 'Controller@method')->name('users.index'); $collector = Collector::create()->collect(); echo $collector->uri('users.index'); // => /users $collector->route('users.index'); // => Instance of RTC\Http\Router\Route\RouteData
Note
- You must be careful when using Collector::collect() and Collector::collectFile() together,
as collectFile method will clear previously collected routes before it starts collecting.
Make sure that you call Collector::collect() first, before calling Collector::collectFile().
Licence
Route http verbs image is owned by Riptutorial.
QuickRoute is MIT licenced.