Recommend this page to a friend! |
![]() ![]() |
Info | ![]() |
![]() |
![]() ![]() |
Reputation | Support forum | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2023-06-25 (2 months ago) ![]() | Not yet rated by the users | Total: 65 | All time: 10,265 This week: 122![]() |
Version | License | PHP version | Categories | |||
simpleroute 1.0 | MIT/X Consortium ... | 8 | HTTP, Design Patterns, PHP 8 |
Description | Author | |
This package can route HTTP requests to callback functions. |
|
> :warning: This project was made for educational purposes only. It is not intended to be used in production environments.
use Mateodioev\HttpRouter\exceptions\{HttpNotFoundException, RequestException};
use Mateodioev\HttpRouter\{Request, Response, Router};
$router = new Router();
// Register your endpoints here
$router->get('/', function (Request $r) {
return Response::text('Hello world!');
});
$router->run();
You can use the Router::get
, Router::post
, Router::put
, Router::patch
and Router::delete
methods to register your endpoints.
$router->myHttpMethod($uri, $callback);
You can map all static files in a directory with the static
method.
$router->static($baseUri, $path, $methods);
// Default methods are GET
Example:
tree
.
??? index.php
??? styles.css
1 directory, 2 files
$router->static('/docs', 'public/');
Now you can reach this uris
You can use the Router::all
method to handle all HTTP methods with one callback.
$router->all($uri, $callback);
You can use path parameters in your endpoints. Path parameters are defined by a bracket followed by the name of the parameter.
> /users/{id}
$router->get('/page/{name}', function (Request $r) {
return Response::text('Welcome to ' . $r->param('name'));
});
Note: You can make a parameter optional by adding a question mark after the name of the parameter.
> /users/{id}?
$router->get('/page/{name}?', function (Request $r) {
$pageName = $r->param('name') ?? 'home'; // If the parameter is not present, the method return null
return Response::text('Welcome to ' . $pageName);
});
You can get all data from the request with the following methods:
TODO list: - [ ] Add support for middlewares. - [ ] Add support for custom error handlers.
![]() |
File | Role | Description | ||
---|---|---|---|---|
![]() |
||||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Example | Example script | ||
![]() ![]() |
Doc. | Documentation |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.