Skip to content
This repository was archived by the owner on Apr 2, 2024. It is now read-only.

Latest commit

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Utilities PHP

Build StatusCoverage StatusCode QualityBuild Status

Latest Stable VersionCode SizeDownloadsLicense

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

<?phprequire'vendor/autoload.php';
useUtilities\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
useUtilities\Routing\Response;
useUtilities\Routing\Router;
useUtilities\Routing\Utils\StatusCode;
Router::post('/', function () {
echo'Hello World!';
});
Create dynamic routes
useUtilities\Routing\Response;
useUtilities\Routing\Router;
useUtilities\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
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",
]);
}
}
Create a controller with dynamic routes
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'],
],
]);
}
}
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.

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'],
],
]);
}
}
Anonymous controllers
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",
]);
}
});
Anonymous controllers with passing extra parameters
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,
]);
}
});

Application

Some documentation will be here.

Create a simple application
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",
]);
}
}

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.

About

🔍 This is a collection of utilities for routing and loading components.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages