A day will come when I will write documentation for this library. Until then, you can use this library to create routes for your application.
composer require utilities-php/routing<?phprequire'vendor/autoload.php';
useUtilities\Routing\Router;
Router::get('/', function () {
return'Hello World!';
});Some documentation will be here.
To add another example, just add the link to the documentation.
Some documentation will be here.
useUtilities\Routing\Response;
useUtilities\Routing\Router;
useUtilities\Routing\Utils\StatusCode;
Router::post('/', function () {
echo'Hello World!';
});useUtilities\Routing\Response;
useUtilities\Routing\Router;
useUtilities\Routing\Utils\StatusCode;
Router::post('/hello/{name}', function ($name) {
Response::send(StatusCode::OK, [
'message' => "Hello {$name}",
]);
});Some documentation will be here.
useUtilities\Routing\Response;
useUtilities\Routing\Utils\StatusCode;
class HomeController extends \Utilities\Routing\Controller
{
publicfunctionindex(): void
{
Response::send(StatusCode::OK, [
'description' => "You are on the index page",
]);
}
}useUtilities\Routing\Attributes\Route;
useUtilities\Routing\Response;
useUtilities\Routing\Attributes\RateLimit;
useUtilities\Routing\Utils\StatusCode;
class HelloController extends \Utilities\Routing\Controller
{
#[RateLimit(500, 1)]
#[Route('GET', '/hello/{name}')]
publicfunctionprint(array$params): void
{
Response::send(StatusCode::OK, [
'result' => [
'name' => $params['name'],
],
]);
}
}Please note that you have to implement the __isAuthorized() method into your controller class, and also you can
rewrite the unauthorized message by implementing the __unauthorized() method.
useUtilities\Routing\Attributes\Route;
useUtilities\Routing\Response;
useUtilities\Routing\Utils\StatusCode;
class PaymentController extends \Utilities\Routing\Controller
{
privatestring$secret = "SOMETHING";
publicfunction__isAuthorized(): bool
{
if ($this->secret === "SOMETHING") {
returntrue;
}
returnfalse;
}
publicfunction__unauthorized(): void
{
Response::send(StatusCode::UNAUTHORIZED,[
'message'=> "Unauthorized: Sorry, your request could not be processed"
]);
}
// NOTE: The third parameter of the `Route` attribute is for defining whether the route is secure or not.
#[Route('POST', '/user/payment', true)]
publicfunctionpay(array$params): void
{
Response::send(StatusCode::OK, [
'result' => [
'name' => $params['name'],
],
]);
}
}useUtilities\Routing\Controller;
useUtilities\Routing\Response;
useUtilities\Routing\Router;
Router::controller('Hello', '/api/hello/{name}', newclassextends Controller {
publicfunctionindex(array$params): void
{
Response::send(200, [
'description' => "You are on the index page",
]);
}
});useUtilities\Routing\AnonymousController;
useUtilities\Routing\Response;
useUtilities\Routing\Router;
useUtilities\Routing\Utils\StatusCode;
Router::controller('Hello', '/api/passing', newclass($something) extends AnonymousController {
publicfunction__process($something): void
{
Response::send(StatusCode::OK, [
'result' => $something,
]);
}
});Some documentation will be here.
useUtilities\Routing\Controller;
useUtilities\Routing\Request;
useUtilities\Routing\Response;
useUtilities\Routing\Utils\StatusCode;
class App extends \Utilities\Routing\Application
{
publicfunction__process(Request$request): void
{
self::addController([
Controller::__create('/api/todo', TodoController::class),
Controller::__create('/api/users', UsersController::class)
]);
}
publicfunction__exception(\Throwable$throwable): void
{
Response::send(StatusCode::INTERNAL_SERVER_ERROR,[
'description' => "Internal Server Error",
]);
}
}Some documentation will be here.
MIT License
Copyright (c) 2022 LiteHex
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

