Router Utilities - PHP
Introduction
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.
Installation
composer require utilities-php/router
Getting Started
<?php
require 'vendor/autoload.php';
use Utilities\Router\Router;
Router::get('/', function () {
return 'Hello World!';
});
Documentation
Some documentation will be here.
Examples
To add another example, just add the link to the documentation.
Router
Some documentation will be here.
Create simple routes
use Utilities\Router\Response;
use Utilities\Router\Router;
use Utilities\Router\Utils\StatusCode;
Router::post('/', function () {
echo 'Hello World!';
});
Create dynamic routes
use Utilities\Router\Response;
use Utilities\Router\Router;
use Utilities\Router\Utils\StatusCode;
Router::post('/hello/{name}', function ($name) {
Response::send(StatusCode::OK, [
'message' => "Hello {$name}",
]);
});
Controller
Some documentation will be here.
Create a simple controller
use Utilities\Router\Response;
use Utilities\Router\Utils\StatusCode;
class HomeController extends \Utilities\Router\Controller
{
public function index(): void
{
Response::send(StatusCode::OK, [
'description' => "You are on the index page",
]);
}
}
Create a controller with dynamic routes
use Utilities\Router\Attributes\Route;
use Utilities\Router\Response;
use Utilities\Router\Attributes\RateLimit;
use Utilities\Router\Utils\StatusCode;
class HelloController extends \Utilities\Router\Controller
{
#[RateLimit(500, 1)]
#[Route('GET', '/hello/{name}')]
public function print(array $params): void
{
Response::send(StatusCode::OK, [
'result' => [
'name' => $params['name'],
],
]);
}
}
Create a controller with secure routes
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.
use Utilities\Router\Attributes\Route;
use Utilities\Router\Response;
use Utilities\Router\Utils\StatusCode;
class PaymentController extends \Utilities\Router\Controller
{
private string $secret = "SOMETHING";
public function __isAuthorized(): bool
{
if ($this->secret === "SOMETHING") {
return true;
}
return false;
}
public function __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)]
public function pay(array $params): void
{
Response::send(StatusCode::OK, [
'result' => [
'name' => $params['name'],
],
]);
}
}
Anonymous controllers
use Utilities\Router\Controller;
use Utilities\Router\Response;
use Utilities\Router\Router;
Router::controller('Hello', '/api/hello/{name}', new class extends Controller {
public function index(array $params): void
{
Response::send(200, [
'description' => "You are on the index page",
]);
}
});
Anonymous controllers with passing extra parameters
use Utilities\Router\AnonymousController;
use Utilities\Router\Response;
use Utilities\Router\Router;
use Utilities\Router\Utils\StatusCode;
Router::controller('Hello', '/api/passing', new class($something) extends AnonymousController {
public function __process($something): void
{
Response::send(StatusCode::OK, [
'result' => $something,
]);
}
});
Application
Some documentation will be here.
Create a simple application
use Utilities\Router\Controller;
use Utilities\Router\Request;
use Utilities\Router\Response;
use Utilities\Router\Utils\StatusCode;
class App extends \Utilities\Router\Application
{
public function __process(Request $request): void
{
self::addController([
Controller::__create('/api/todo', TodoController::class),
Controller::__create('/api/users', UsersController::class)
]);
}
public function __exception(\Throwable $throwable): void
{
Response::send(StatusCode::INTERNAL_SERVER_ERROR,[
'description' => "Internal Server Error",
]);
}
}
Redirection
Some documentation will be here.
License
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.