vilnis/easy-route 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

vilnis/easy-route

最新稳定版本:v1.0.0

Composer 安装命令:

composer require vilnis/easy-route

包简介

Easy to use PHP router.

README 文档

README

EasyRoute is a lightweight PHP router for handling HTTP requests and routing them to the appropriate controller methods.

Features

  • Simple and easy-to-use routing mechanism
  • Supports dynamic routes with parameters
  • Compatible with PHP 8.1.0 or later

Installation

You can install EasyRoute via Composer. Run the following command in your terminal:

composer require vilnis/easy-route

Usage

Basic Usage

<?php
use EasyRoute\Router;
use EasyRoute\Exceptions\RouteNotFoundException;
use EasyRoute\Exceptions\MethodNotAllowedException;
use MyCustomNamespace\MyCustomController; // Replace with your custom namespace

// Include Composer's autoloader
require_once __DIR__ . '/vendor/autoload.php';

// Create a new router instance
$router = new EasyRoute\Router();

// Define routes using addRoute() with HTTP methods
$router->addRoute('GET', '/users', [MyCustomController::class, 'index']);
$router->addRoute('POST', '/users', [MyCustomController::class, 'create']);
$router->addRoute('PUT', '/users/{id/d}/{text/t}', [MyCustomController::class, 'update']);
$router->addRoute('PATCH', '/users/{id/d}', [MyCustomController::class, 'modify']);
$router->addRoute('DELETE', '/users/{id/d}/{text/t}', [MyCustomController::class, 'delete']);

try {
    // Get request method and URI
    $method = $_SERVER['REQUEST_METHOD'];
    $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

    // Match the route
    [$routeHandler, $params] = $router->match($method, $uri);

    if ($routeHandler !== null) {
        // Execute the corresponding method based on the matched route
        [$controller, $method] = $routeHandler;
        $controllerInstance = new $controller();

        // Handle the matched route with extracted parameters dynamically as an array
        $response = call_user_func_array([$controllerInstance, $method], [$params]);

        // Output the response or handle it further
        echo $response;
    } else {
        throw new RouteNotFoundException('Route not found.');
    }
} catch (RouteNotFoundException $e) {
    // Handle RouteNotFoundException
    http_response_code(404);
    echo '404 Not Found: ' . $e->getMessage();
} catch (MethodNotAllowedException $e) {
    // Handle MethodNotAllowedException
    http_response_code(405);
    echo '405 Method Not Allowed: ' . $e->getMessage();
}

Routes with Type Markers (/d and /t):

  • /users/{id/d}/{text/t}: Expects two parameters:

    • {id/d}: Requires a digit (numeric value) for the id.
    • {text/t}: Expects alphabetic text.
  • /users/{id/d}: Expects a single parameter:

    • {id/d}: Requires a digit (numeric value) for the id.

Routes without Type Markers:

  • /products/{category}: This route includes {category} parameter without specific type markers {category/t} or {category/d}.
    • {category}: Accepts various types of parameters (digits, alphabetic characters, etc.).

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-12-30