utilities-php/routing

该软件包已被放弃,不再维护。未建议替代软件包。

PHP路由器

dev-master 2022-10-22 07:57 UTC

This package is auto-updated.

Last update: 2024-08-17 00:00:56 UTC


README

Utilities PHP

Build Status Coverage Status Code Quality Build Status

Latest Stable Version Code Size Downloads License

路由器工具 - PHP

简介

总有一天我会为这个库编写文档。在此之前,您可以使用这个库为您的应用程序创建路由。

安装

composer require utilities-php/routing

入门

<?php
require 'vendor/autoload.php';

use Utilities\Routing\Router;

Router::get('/', function () {
    return 'Hello World!';
});

文档

一些文档将在这里。

示例

要添加另一个示例,只需将链接添加到文档中。


路由器

一些文档将在这里。

创建简单路由
use Utilities\Routing\Response;
use Utilities\Routing\Router;
use Utilities\Routing\Utils\StatusCode;

Router::post('/', function () {
    echo 'Hello World!';
});
创建动态路由
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}",
    ]);
});

控制器

一些文档将在这里。

创建一个简单的控制器
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",
        ]);
    }

}
创建具有动态路由的控制器
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'],
            ],
        ]);
    }

}
创建具有安全路由的控制器

请注意,您必须将 __isAuthorized() 方法实现到您的控制器类中,并且您还可以通过实现 __unauthorized() 方法来重写未授权消息。

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'],
            ],
        ]);
    }

}
匿名控制器
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",
        ]);
    }

});
带额外参数的匿名控制器
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,
        ]);
    }

});

应用程序

一些文档将在这里。

创建一个简单应用程序
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",
        ]);
    }

}

重定向

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.