codememory/routing

v1.4 2022-01-14 15:51 UTC

This package is auto-updated.

Last update: 2024-09-14 21:51:16 UTC


README

安装

composer require codememory/routing

安装包后,必须执行以下命令:

  • 如果不存在,则创建全局配置
    • php vendor/bin/gc-cdm g-config:init
  • 合并所有配置
    • php vendor/bin/gc-cdm g-config:merge --all

文件夹 .config 存储了全局包 codememory 的配置

配置概览

# configs/routing.yaml

routing:
  _settings:
    # Path with route files
    pathWithRoutes: App/Routing/Routes/
    
    # Namespace for Software
    softwareNamespace: Codememory\Routing\App\Routing\Software\
    
    # Suffix for file with routes
    routesFileSuffix: null
  
  # List added routes
  _routes:
    # Route name
    test:
      path: 'test/:id' # Route path, with parameter id
      method: 'GET'    # HTTP Method
      class:           # Handler for route
        controller: Codememory\Routing\App\Controllers\TestController
        method: main
      # Regular Expressions for Route Path Parameters
      parameters:
        id: '\d+'
        name: '[a-zA-Z]+'
      # Route software
      software:
        Auth: api    # SoftwareName:MethodName
        CheckIp: api
      schemes:
        - http
        - https

安装后,只需调用 __constructStatic 方法,然后调用 processAllRoutes 即可。

如果您希望从文件中加载路由,可以在调用 processAllRoutes 之前调用方法 initializingRoutesFromConfig

初始化示例

不使用文件初始化路由

<?php

use Codememory\Routing\Router;
use Codememory\HttpFoundation\Request\Request;

require_once 'vendor/autoload.php';

Router::__constructStatic(new Request());
Router::processAllRoutes();

从配置和文件中初始化路由

<?php

use Codememory\Routing\Router;
use Codememory\HttpFoundation\Request\Request;

require_once 'vendor/autoload.php';

Router::__constructStatic(new Request());
Router::initializingRoutesFromConfig();
Router::processAllRoutes();

路由使用示例。请参阅文件 .example