Download the PHP package lkt/http-router without Composer
On this page you can find all versions of the php package lkt/http-router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package http-router
About this package
This package is designed to handle HTTP request.
All Response objects used by the Router are described in lkt/http-response.
This package implements nikic/FastRoute so all dynamic params for routes are available.
Installation
Register routes
Register a new route is as easy as instantiate a class. It takes two arguments: the full route and a callable with the Route handler.
Keep in mind a route can't be declared twice.
Routes visibility
All routes have two alternative constructors to control if it's available in a public or private way: onlyLoggedUsers and onlyNotLoggedUsers
Determine if user is logged in
Router includes a method which accepts a callable 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.
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:
Restricting even more the access to a route
Every Route can add some access checkers to determine if it's accessible:
The access checker handler will receive an array with all request variables in it and can have some different returns:
- Returning a
Responseinstance will makeRouterdispatch this Response. false, which means user is not allowed andRouterwill dispatch a forbidden responsevoid, the access checker doesn't limit the access to data andRouterwill work as usually
Routes handlers
Router expects all routes returns a Response instance (see lkt/http-response).
Each handler will receive as first argument an array with all request variables in it.
An example of YourController.php:
Route resolving
In your index.php, simple add this:
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 to know what can be done with the Response instance, for example, how to send HTTP headers.
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.
Token detection
Router can give you the HTTP_TOKEN or a Bearer Token. Both methods return null if the token is undefined.
All versions of http-router with dependencies
nikic/fast-route Version ^1.3
lkt/http-response Version ^2.0
lkt/factory-schemas Version ^3.0
lkt/factory-instantiator Version ^3.0
lkt/users Version ^1.0
lkt/web-items Version ^1.0