Recommend this page to a friend! |
![]() |
Info | Example | ![]() |
![]() |
![]() |
Reputation | Support forum | Blog | Links |
Ratings | Unique User Downloads | Download Rankings | ||||
Not enough user ratings | Total: 79 | All time: 10,129 This week: 61![]() |
Version | License | PHP version | Categories | |||
simpleroute 1.0.0 | MIT/X Consortium ... | 8 | HTTP, Design Patterns, PHP 8 |
Description | Author | |
This package can route HTTP requests to callback functions. |
|
<?php |
> :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 |
The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. |
![]() |
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.