law909/altorouter

此包最新版本(1.0.7)没有可用的许可信息。

PHP 的闪电路由器

1.0.7 2023-01-09 01:41 UTC

This package is auto-updated.

Last update: 2024-09-09 05:26:38 UTC


README

AltoRouter 是一个轻量但强大的 PHP 5.3+ 路由类,深受 klein.php 的启发。

  • 动态路由带命名参数
  • 反向路由
  • 灵活的正则表达式路由(受 Sinatra 启发)

入门

  1. 需要 PHP 5.3.x
  2. 设置 URL 重写,使所有请求都由 index.php 处理
  3. 创建一个 AltoRouter 实例,映射你的路由并匹配请求。
  4. 查看提供的示例文件(index.php),以更好地了解如何使用 AltoRouter。

路由

$router = new AltoRouter();
$router->setBasePath('/AltoRouter');

// mapping routes
$router->map('GET|POST','/', 'home#index', 'home');
$router->map('GET','/users/', array('c' => 'UserController', 'a' => 'ListAction'));
$router->map('GET','/users/[i:id]', 'users#show', 'users_show');
$router->map('POST','/users/[i:id]/[delete|update:action]', 'usersController#doAction', 'users_do');


// reversed routing
$router->generate('users_show', array('id' => 5));

您可以在命名参数上使用以下限制。AltoRouter 将创建正确的正则表达式。

    *                    // Match all request URIs
    [i]                  // Match an integer
    [i:id]               // Match an integer as 'id'
    [a:action]           // Match alphanumeric characters as 'action'
    [h:key]              // Match hexadecimal characters as 'key'
    [:action]            // Match anything up to the next / or end of the URI as 'action'
    [create|edit:action] // Match either 'create' or 'edit' as 'action'
    [*]                  // Catch all (lazy, stops at the next trailing slash)
    [*:trailing]         // Catch all as 'trailing' (lazy)
    [**:trailing]        // Catch all (possessive - will match the rest of the URI)
    .[:format]?          // Match an optional parameter 'format' - a / or . before the block is also optional

Some more complicated examples

    /posts/[*:title][i:id]     // Matches "/posts/this-is-a-title-123"
    /output.[xml|json:format]? // Matches "/output", "output.xml", "output.json"
    /[:controller]?/[:action]? // Matches the typical /controller/action format

附加信息

如果您喜欢 AltoRouter,您可能也会喜欢 PHP Router

许可

(MIT 许可)

版权 (c) 2012 Danny van Kooten dannyvankooten@gmail.com

在此条件下,任何人获取此软件及其相关文档文件(“软件”)的副本,均可免费使用、处理该软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本,并允许向提供软件的人使用软件进行上述操作:

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

软件按“现状”提供,不提供任何明示或暗示的保证,包括但不限于适销性、针对特定目的的适用性和非侵权性保证。在任何情况下,作者或版权所有者不对任何索赔、损害或其他责任负责,无论这些责任是因合同、侵权或其他原因而引起的,无论是在软件或软件的使用或其它交易中。