PHP code example of lkt / http-router

1. Go to this page and download the library: Download lkt/http-router 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/ */

    

lkt / http-router example snippets


use Lkt\Http\Routes\GetRoute;
use Lkt\Http\Routes\PostRoute;
use Lkt\Http\Routes\PutRoute;
use Lkt\Http\Routes\PatchRoute;
use Lkt\Http\Routes\DeleteRoute;

GetRoute::register('/blog/{id}', [YourController::class, 'handler']); // Route only can be accessed by GET
PostRoute::register('/blog', [YourController::class, 'handler']); // Route only can be accessed by POST
PutRoute::register('/blog/{id}', [YourController::class, 'handler']); // Route only can be accessed by PUT
PatchRoute::register('/blog/{id}', [YourController::class, 'handler']); // Route only can be accessed by PATCH
DeleteRoute::register('/blog/{id}', [YourController::class, 'handler']); // Route only can be accessed by DELETE

use Lkt\Http\Routes\GetRoute;
use Lkt\Http\Routes\PostRoute;
use Lkt\Http\Routes\PutRoute;
use Lkt\Http\Routes\PatchRoute;
use Lkt\Http\Routes\DeleteRoute;

GetRoute::onlyLoggedUsers('/blog/{id}', [YourController::class, 'handler']);
PostRoute::onlyLoggedUsers('/blog', [YourController::class, 'handler']);
PutRoute::onlyLoggedUsers('/blog/{id}', [YourController::class, 'handler']);
PatchRoute::onlyLoggedUsers('/blog/{id}', [YourController::class, 'handler']);
DeleteRoute::onlyLoggedUsers('/blog/{id}', [YourController::class, 'handler']);

GetRoute::onlyNotLoggedUsers('/public-blog/{id}', [YourController::class, 'handler']);
PostRoute::onlyNotLoggedUsers('/public-blog', [YourController::class, 'handler']);
PutRoute::onlyNotLoggedUsers('/public-blog/{id}', [YourController::class, 'handler']);
PatchRoute::onlyNotLoggedUsers('/public-blog/{id}', [YourController::class, 'handler']);
DeleteRoute::onlyNotLoggedUsers('/public-blog/{id}', [YourController::class, 'handler']);

use Lkt\Http\Router;

// With a function
Router::setLoggedUserChecker(function(){
    // Do your stuff
    return true;
});

// With a callable array
Router::setLoggedUserChecker([YourLoginController::class, 'yourLoginCheckerMethod']);

use Lkt\Http\Routes\GetRoute;

GetRoute::onlyLoggedUsers('/blog', [YourController::class, 'handler'])
    ->setLoggedUserChecker([YourLoginController::class, 'yourSpecificLoginCheckerMethod']);

use Lkt\Http\Routes\GetRoute;

GetRoute::onlyLoggedUsers('/blog', [YourController::class, 'handler'])
    ->addAccessChecker([YourLoginController::class, 'checkThisUserIsAdmin']);

use Lkt\Http\Response;

class YourController {
    public static function index(array $params = []): Response
    {
        return Response::ok(['message' => 'everything ok!']);
    }
}


// ...
// And that method would be mapped this way:
GetRoute::onlyLoggedUsers('/blog', [YourController::class, 'index']);

use Lkt\Http\Router;

Router::dispatch();

use Lkt\Http\Router;

Router::getResponse();


use Lkt\Http\Router;
use Lkt\Http\Response;

$response = Response::serviceUnavailable('Server under maintenance');

Router::forceGlobalResponse($response);


use Lkt\Http\Router;

Router::getBearerToken();
Router::getTokenHeader();