PHP code example of ahmard / quick-route

1. Go to this page and download the library: Download ahmard/quick-route library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ahmard / quick-route example snippets


use QuickRoute\Route;
use QuickRoute\Router\Dispatcher;

);

$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]+'
]);

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;

    ->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');

use QuickRoute\Crud;

Crud::create('/', 'Controller')->go();

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();

use QuickRoute\Router\Collector;

$collector = Collector::create()
    ->collectFile('routes.php')
    ->cache('path/to/save/cache.php', false)
    ->register();

$routes = $collector->getCollectedRoutes();

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();

use QuickRoute\Router\Collector;

$collector = Collector::create();
$collector->collectFile('api-routes.php', [
    'prefix' => 'api',
    'name' => 'api.',
    'namespace' => 'Api\\'
]);
$collector->register();

use QuickRoute\Route;
use QuickRoute\Router\Collector;
use QuickRoute\Router\Dispatcher;

nt('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