An elegant http router built on top of FastRoute to provide more easy of use.
Check ChangeLog file
composer require ahmard/quick-route
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;
}
use QuickRoute\Route;
Route::get('/home', 'MainController@home');
use QuickRoute\Route;
Route::prefix('user')->name('user.')
->namespace('User')
->middleware('UserMiddleware')
->group(function (){
Route::get('profile', 'UserController@profile');
Route::put('update', 'UserController@update');
});
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}
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]+'
]);
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'));
});
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());
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');
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');
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');
use QuickRoute\Route;
Route::resource('photos', 'App\Http\Controller\PhotoController');
Code above will produce below routes
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.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();
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
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();
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);
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
- 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().
Route http verbs image is owned by Riptutorial.
QuickRoute is MIT licenced.