celiovmjr/simplerouter 问题修复 & 功能扩展

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

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

celiovmjr/simplerouter

最新稳定版本:2.0.8

Composer 安装命令:

composer require celiovmjr/simplerouter

包简介

A lightweight, easy-to-use PHP router for building web applications.

README 文档

README

SimpleRouter is a PHP class for managing routes in a PHP web application in a simple and effective manner.

Installation

To use SimpleRouter, simply include the Router.php file in your PHP project and configure the appropriate autoloading.

<?php
require_once 'Router.php';

Example Usage

Initial Setup

use SimpleRouter\Application\Router;

$router = new Router();

Defining Routes

Basic Routes

$router->get('/', function($request) {
    echo "Hello, GET request!";
});

$router->post('/post', [MyController::class, 'postMethod']);

Route Groups and Subgroups

$router->group('/api', function() use ($router) {
    // Subgroup '/v2'
    $router->subgroup('/v2', function() use ($router) {
        $router->get('/users', [UserController::class, 'getAll']);
        $router->post('/users', [UserController::class, 'create']);
    });

    // You can define more routes directly within the '/api' group
    $router->get('/status', function($request) {
        echo "API Status";
    });
});

Naming Routes

$router->group('/admin', function() use ($router) {
    $router->get('/dashboard', function($request) {
        echo "Welcome to Admin Dashboard!";
    })->name('admin.dashboard');
});

// Resolving a named route
$route = $router->route('admin.dashboard'); // Returns '/admin/dashboard'

Running the Application

if ($router->dispatch()) {
    // Route found and processed successfully
} else {
    // Error handling
    $error = $router->error();
    echo "Error {$error->code}: {$error->message}";
}

Available Methods

  • get($uri, $handler): Defines a GET route.
  • post($uri, $handler): Defines a POST route.
  • put($uri, $handler): Defines a PUT route.
  • patch($uri, $handler): Defines a PATCH route.
  • delete($uri, $handler): Defines a DELETE route.
  • group($prefix, $handler): Defines a route group with a prefix.
  • subgroup($prefix, $handler): Defines a subgroup of routes within an existing group.
  • dispatch(): Executes routing and processes the corresponding route.
  • route($name): Returns the URI associated with a named route.
  • error(): Returns an stdClass object with error information, if any.

Benefits

  • Modular Routing: Easily organize routes into groups and subgroups to maintain a structured application architecture.
  • Flexible Routing: Supports dynamic and static routes, closures, and controller methods.
  • Error Handling: Provides error handling for route dispatching failures.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-06-21