๐Ÿ” This is a collection of utilities for routing and loading components.

Overview

Utilities PHP

Build Status Coverage Status Code Quality Build Status

Latest Stable Version Code Size Downloads License

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.

You might also like...
Pux is a fast PHP Router and includes out-of-box controller tools

Pux Pux is a faster PHP router, it also includes out-of-box controller helpers. 2.0.x Branch Build Status (This branch is under development) Benchmark

Toro is a PHP router for developing RESTful web applications and APIs.

Toro Toro is a PHP router for developing RESTful web applications and APIs. It is designed for minimalists who want to get work done. Quick Links Offi

A lightweight and simple object oriented PHP Router

bramus/router A lightweight and simple object oriented PHP Router. Built by Bram(us) Van Damme (https://www.bram.us) and Contributors Features Support

route:menu gives you a beautiful route list which is friendly on smaller terminals and brings a few new features in.
route:menu gives you a beautiful route list which is friendly on smaller terminals and brings a few new features in.

Laravel Route Menu Your route:list, sir. route:menu gives you a beautiful route list which is friendly on smaller terminals and brings a few new featu

PhpRouter is a powerful, lightweight, and very fast HTTP URL router for PHP projects.

PhpRouter PhpRouter is a powerful, lightweight, and very fast HTTP URL router for PHP projects. Some of the provided features: Route parameters Predef

A lightweight and fast router for PHP
A lightweight and fast router for PHP

Piko Router A lightweight and blazing fast router (see benchmarks) using a radix trie to store dynamic routes. This router maps routes to user defined

Simple and minimal yet another PHP 7 Framework

DemirApp Minimal PHP Framework Introduction Simple and minimal yet another PHP 7 Framework Features Simple routing Simple container (Dependency Inject

Thruway - an open source client and router implementation of WAMP (Web Application Messaging Protocol), for PHP.

PHP Client and Router Library for Autobahn and WAMP (Web Application Messaging Protocol) for Real-Time Application Messaging

OpenAPI (Swagger) Specification Support for Sunrise Router (and not only)

OpenAPI (Swagger) Specification Support for Sunrise Router Important to understanding OpenAPI Specification Installation composer require 'sunrise/htt

Comments
Owner
Utilities for PHP
A PHP utility library that makes your coding process faster and easier.
Utilities for PHP
Routing - The Routing component maps an HTTP request to a set of configuration variables.

Routing Component The Routing component maps an HTTP request to a set of configuration variables. Getting Started $ composer require symfony/routing

Symfony 7.3k Jan 6, 2023
Fast PSR-7 based routing and dispatch component including PSR-15 middleware, built on top of FastRoute.

Route This package is compliant with PSR-1, PSR-2, PSR-4, PSR-7, PSR-11 and PSR-15. If you notice compliance oversights, please send a patch via pull

The League of Extraordinary Packages 608 Dec 30, 2022
PHPRouter is an easy-to-use, fast, and flexible PHP router package with express-style routing.

PHP-Router is a modern, fast, and adaptable composer package that provides express-style routing in PHP without a framework.

Ayodeji O. 4 Oct 20, 2022
Generate a PHP script for faster routing :rocket:

SwitchRoute Generating a PHP script for faster routing. The traditional way of routing uses regular expressions. This method was improved by FastRoute

Arnold Daniels 75 Nov 20, 2022
Convention based routing for PHP

Croute Convention based routing for PHP based on Symfony components. Croute is great because: You don't need to maintain a routing table Promotes cons

Michael O'Connell 12 Nov 9, 2021
PHP routing (like laravel) (not complete yet)

PHP Router (under construction) This repository contains routing classes that enables you to define your routes similar to laravel 8 routes. Features

Kareem M. Fouad 6 Jan 16, 2022
Flight routing is a simple, fast PHP router that is easy to get integrated with other routers.

The PHP HTTP Flight Router divineniiquaye/flight-routing is a HTTP router for PHP 7.1+ based on PSR-7 and PSR-15 with support for annotations, created

Divine Niiquaye Ibok 16 Nov 1, 2022
Ertuo: quick routing for PHP

Ertuo: quick routing for PHP Ertuo (anagram of "Route"), is a small PHP library that does routing better and faster than conventional regular expressi

Ertuo 29 Jul 19, 2022
:tada: Release 2.0 is released! Very fast HTTP router for PHP 7.1+ (incl. PHP8 with attributes) based on PSR-7 and PSR-15 with support for annotations and OpenApi (Swagger)

HTTP router for PHP 7.1+ (incl. PHP 8 with attributes) based on PSR-7 and PSR-15 with support for annotations and OpenApi (Swagger) Installation compo

Sunrise // PHP 151 Jan 5, 2023
Simple, fast and yet powerful PHP router that is easy to get integrated and in any project.

Simple, fast and yet powerful PHP router that is easy to get integrated and in any project. Heavily inspired by the way Laravel handles routing, with both simplicity and expand-ability in mind.

Simon Sessingรธ 472 Jan 4, 2023