PHPackages                             lkt/http-router - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. lkt/http-router

ActiveLibrary[HTTP &amp; Networking](/categories/http)

lkt/http-router
===============

LKT HTTP Router

2.0.7(3mo ago)02721MITPHPPHP &gt;=8.1.0

Since Dec 28Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/lkt-php/lkt-http-router)[ Packagist](https://packagist.org/packages/lkt/http-router)[ RSS](/packages/lkt-http-router/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (22)Used By (1)

About this package
==================

[](#about-this-package)

This package is designed to handle HTTP request.

All `Response` objects used by the `Router` are described in [lkt/http-response](https://github.com/lekrat/lkt-http-response).

This package implements [nikic/FastRoute](https://github.com/nikic/FastRoute) so all dynamic params for routes are available.

Installation
============

[](#installation)

```
composer require lkt/http-router
```

Register routes
===============

[](#register-routes)

Register a new route is as easy as instantiate a class. It takes two arguments: the full route and a [callable](https://www.php.net/manual/en/language.types.callable.php) with the `Route` handler.

```
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
```

Keep in mind a route can't be declared twice.

Routes visibility
=================

[](#routes-visibility)

All routes have two alternative constructors to control if it's available in a public or private way: `onlyLoggedUsers` and `onlyNotLoggedUsers`

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

Determine if user is logged in
==============================

[](#determine-if-user-is-logged-in)

`Router` includes a method which accepts a [callable](https://www.php.net/manual/en/language.types.callable.php) function to check if user is logged in or not.

This function must return a `bool` value (`true` means user is logged).

This way, `Router` doesn't modify your logic.

```
use Lkt\Http\Router;

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

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

Specify a custom login checker for a `Route`
============================================

[](#specify-a-custom-login-checker-for-a-route)

If you have a context where can exist many ways to get logged in, you can specify a custom login checker:

```
use Lkt\Http\Routes\GetRoute;

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

Restricting even more the access to a route
===========================================

[](#restricting-even-more-the-access-to-a-route)

Every `Route` can add some access checkers to determine if it's accessible:

```
use Lkt\Http\Routes\GetRoute;

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

The access checker handler will receive an array with all request variables in it and can have some different returns:

- Returning a `Response` instance will make `Router` dispatch this Response.
- `false`, which means user is not allowed and `Router` will dispatch a forbidden response
- `void`, the access checker doesn't limit the access to data and `Router` will work as usually

Routes handlers
===============

[](#routes-handlers)

`Router` expects all routes returns a `Response` instance (see [lkt/http-response](https://github.com/lekrat/lkt-http-response)).

Each handler will receive as first argument an array with all request variables in it.

An example of YourController.php:

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

Route resolving
===============

[](#route-resolving)

In your `index.php`, simple add this:

```
use Lkt\Http\Router;

Router::dispatch();
```

The `dispatch` method automatically detects if you're sending a JSON, HTML, a file, ... and disposes the right headers.

Also, the `dispatch` method will end the script execution.

If you have an existing routing engine and want to migrate step by step, you can get the response and works with it. Take a look to [lkt/http-response](https://github.com/lekrat/lkt-http-response) to know what can be done with the `Response` instance, for example, how to send HTTP headers.

```
use Lkt\Http\Router;

Router::getResponse();
```

Globally force a response
=========================

[](#globally-force-a-response)

This feature is useful if you want to return a maintenance status, or block a given IP address, or any situation in which you need to send the same `Response` no matter which route is accessed.

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

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

Router::forceGlobalResponse($response);
```

Token detection
===============

[](#token-detection)

`Router` can give you the HTTP\_TOKEN or a Bearer Token. Both methods return null if the token is undefined.

```
use Lkt\Http\Router;

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

###  Health Score

44

—

FairBetter than 92% of packages

Maintenance80

Actively maintained with recent releases

Popularity11

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~59 days

Recently: every ~3 days

Total

20

Last Release

107d ago

Major Versions

1.0.12 → 2.0.02026-01-06

### Community

Maintainers

![](https://www.gravatar.com/avatar/0915bfa65bb0c27057b7043d2b423137915e107411dd1684630508f2df076291?d=identicon)[lkt](/maintainers/lkt)

---

Top Contributors

[![alphaibanez](https://avatars.githubusercontent.com/u/24976472?v=4)](https://github.com/alphaibanez "alphaibanez (14 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/lkt-http-router/health.svg)

```
[![Health](https://phpackages.com/badges/lkt-http-router/health.svg)](https://phpackages.com/packages/lkt-http-router)
```

###  Alternatives

[league/route

Fast routing and dispatch component including PSR-15 middleware, built on top of FastRoute.

6633.1M116](/packages/league-route)[simple-swoole/simps

A simple, lightweight and high-performance PHP coroutine framework.

4798.0k3](/packages/simple-swoole-simps)[middlewares/fast-route

Middleware to use FastRoute

96191.1k15](/packages/middlewares-fast-route)[amphp/http-server-router

Routes to request handlers based on HTTP method and path for amphp/http-server.

39496.2k28](/packages/amphp-http-server-router)[mezzio/mezzio-fastroute

FastRoute integration for Mezzio

162.7M52](/packages/mezzio-mezzio-fastroute)[hyperf/http-server

A HTTP Server for Hyperf.

102.8M305](/packages/hyperf-http-server)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
