ahmard / quick-route
Simple http router designed to look just like Laravel router
Installs: 1 253
Dependents: 2
Suggesters: 0
Security: 0
Stars: 11
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=7.4
- ext-json: *
- nikic/fast-route: ^1.3
- opis/closure: ^3.6
Requires (Dev)
- phpstan/phpstan: ^1.4
- phpunit/phpunit: ^9.5
- symfony/var-dumper: ^6.0
- dev-master
- 3.9.0
- v3.8.x-dev
- 3.8.3
- 3.8.2
- 3.8.1
- 3.8.0
- v3.7.x-dev
- 3.7.3
- 3.7.2
- 3.7.1
- 3.7.0
- v3.6.x-dev
- 3.6.0
- v3.5.x-dev
- 3.5.3
- 3.5.2
- 3.5.1
- 3.5.0
- v3.4.x-dev
- 3.4.1
- 3.4.0
- v3.3.x-dev
- 3.3.1
- 3.3.0
- v3.2.x-dev
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- v3.1.x-dev
- 3.1.1
- 3.1.0
- 3.0.0
- 2.5.6
- 2.5.5
- 2.5.4
- 2.5.3
- 2.5.2
- 2.5.1
- 2.5.0
- 2.4.0
- 2.3.0
- 2.2.1
- 2.2.0
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.0
- 1.0.2
- 1.0.1
- 1.0.0
- dev-upgrade-middleware-collector
- dev-theroute-params-rename
This package is auto-updated.
Last update: 2024-11-11 06:33:01 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 ahmard/quick-route
Usage
Simple example
use QuickRoute\Route; use QuickRoute\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 QuickRoute\Route; Route::get('/home', 'MainController@home');
Advance usage
use QuickRoute\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 QuickRoute\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 QuickRoute\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 QuickRoute\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 QuickRoute\Route; use QuickRoute\Router\Collector; use QuickRoute\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 QuickRoute\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 QuickRoute\Route; use QuickRoute\Router\Collector; use QuickRoute\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 QuickRoute\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 QuickRoute\Route; Route::resource('photos', 'App\Http\Controller\PhotoController');
Code above will produce below routes
Crud::create()
use QuickRoute\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 QuickRoute\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 QuickRoute\Route; Route::get('/', 'MainController@index'); Route::get('/help', 'MainController@help'); //server.php use QuickRoute\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 QuickRoute\Router\Collector; $collector = Collector::create() ->collectFile('routes.php') ->cache('path/to/save/cache.php', false) ->register(); $routes = $collector->getCollectedRoutes();
Caching routes with closure
use QuickRoute\Route; use QuickRoute\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 QuickRoute\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 QuickRoute\Route; use QuickRoute\Router\Collector; use QuickRoute\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 QuickRoute\Route; use QuickRoute\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 QuickRoute\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.