utilities-php / routing
This package is abandoned and no longer maintained.
No replacement package was suggested.
Router for PHP
dev-master
2022-10-22 07:57 UTC
Requires
- php: >=8.0
- ext-fileinfo: *
- symfony/mime: ^v6.1.6
- utilities-php/auth: *
- utilities-php/common: *
This package is auto-updated.
Last update: 2024-08-17 00:00:56 UTC
README
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/routing
Getting Started
<?php require 'vendor/autoload.php'; use Utilities\Routing\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\Routing\Response; use Utilities\Routing\Router; use Utilities\Routing\Utils\StatusCode; Router::post('/', function () { echo 'Hello World!'; });
Create dynamic routes
use Utilities\Routing\Response; use Utilities\Routing\Router; use Utilities\Routing\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\Routing\Response; use Utilities\Routing\Utils\StatusCode; class HomeController extends \Utilities\Routing\Controller { public function index(): void { Response::send(StatusCode::OK, [ 'description' => "You are on the index page", ]); } }
Create a controller with dynamic routes
use Utilities\Routing\Attributes\Route; use Utilities\Routing\Response; use Utilities\Routing\Attributes\RateLimit; use Utilities\Routing\Utils\StatusCode; class HelloController extends \Utilities\Routing\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\Routing\Attributes\Route; use Utilities\Routing\Response; use Utilities\Routing\Utils\StatusCode; class PaymentController extends \Utilities\Routing\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\Routing\Controller; use Utilities\Routing\Response; use Utilities\Routing\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\Routing\AnonymousController; use Utilities\Routing\Response; use Utilities\Routing\Router; use Utilities\Routing\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\Routing\Controller; use Utilities\Routing\Request; use Utilities\Routing\Response; use Utilities\Routing\Utils\StatusCode; class App extends \Utilities\Routing\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.