emma/http-manager

PHP 8.0+ 基于属性的 HTTP 路由。可用于所有场景,包括 MVC 和 Middleware - 类/方法/函数

v1.0.6 2023-11-17 14:26 UTC

This package is auto-updated.

Last update: 2024-09-17 16:16:54 UTC


README

基于属性的 Http Routing,PHP 8。可用于所有场景,包括 MVC 和 Middleware - 类/方法/函数

快速入门

===================

<?php

include dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . "autoloader.php"; //composer autoloader

// <!-- Controller Class file -->
use Emma\Http\Mappings\RequestMapping;
use Emma\Http\Mappings\PostMapping;
use Emma\Http\Mappings\GetMapping;
use Emma\Http\Request\Method;

#[RequestMapping(routes: '/index', httpRequestMethod: [Method::POST, Method::GET])]
class IndexController
{

    /**
     *   Full Routing to this method is: '/index/login' -> Class level routing plus the method level
     *   Class Method can only be accessed via HTTP - POST
     */
    #[PostMapping('/login')] 
    public function login()
    {

    }

    /**
     * Class Method can be accessed via HTTP - POST and GET
     */ 
    #[RequestMapping('/logout', [Method::POST, Method::GET])]
    public function logout()
    {

    }

    /**
     * Class Method can be accessed via HTTP - GET
     * Auto-Map expected url parameter to the method
     */ 
    #[GetMapping('/count-trades/{status:[\w]+}')]
    public function countTradesByStatus(string $status)
    {

    }

    /**
    * Other Request Mapping Attributes exist....For example:
    *
    *   #[HeadMapping('/head-method-routing')]
    *   #[PutMapping('/upload')]
    *   #[PatchMapping('//summary/{id:[0-9]*}')]
    *   #[DeleteMapping('/delete-all')]
    *   #[OptionsMapping('/option/')]
    */

}

使用单例注册所有路由处理器 - RouteRegistry.php

/**

 * 
 * @return array 
 * Register your Controllers, classes and/or Middleware and/or Functions here...

    use \Emma\Http\Mappings\PatchMapping;

    $routables = [
        IndexController::class,

        ...

        //example: Adding your function directly to the array.  

        #[PatchMapping('/update/summary/{id:[0-9]*}')]
        function middlewareQuickPatch(): void {
            $result = ['status' => true, 'data' => 'ABCD'];
            die(json_encode($result));
        },
    ];

    RouteRegistry::getInstance()->setRoutables($routables); //Register ALL

    

 *    ADVANCED USERS can have there arrays of functions and/or classes in different file and includes those file with array_merge()
 *    For Example:
 
    $routables = array_merge(
        (array) include "directory_to_array_file/class_file.php",
        (array) include "directory_to_array_file/direct_method_file.php",
        (array) include "directory_to_array_file/functions_file.php",
    );

    RouteRegistry::getInstance()->setRoutables($routables);  //Register ALL

* THEN, class_file.php:
* =====================
    return [
        IndexController::class,
        ...
    ];

    Other file will simply follow class_file example...
 */




// <!-- URL front entry point...Assuming you already setup your .htaccess as needed. 
// This service is independent of .htaccess. -->

您可以使用以下方式一次性注册单个可路由项:

RouteRegistry::getInstance()->register($classOrFunctionOrObject);

请确保您的 .htaccess 文件设置正确,因为这个包是框架无关的。

以下是一个示例 .htaccess 文件:

RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.+)$
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]

让我们测试一下 - 同文件夹中的 index.php 文件

<?php

$testHttpManager = new \Emma\Http\HttpManager();

try {
    $route = $testHttpManager->boot()->matchRequestRoutes();

    var_dump($route);

    /** Feel free to use https://github.com/debascoguy/Di For Autowiring(that is, Injecting) your classes/Methods/Function Dependencies... */

} catch (Exception $e) {

    die($e->getMessage());

}

** 可以免费使用 https://github.com/debascoguy/Di 进行自动装配(即注入)您的类/方法/函数依赖关系...