elzekool/koolrouter

支持RESTful和反向路由的快速灵活的PHP5.3+路由器。

0.2.0 2013-07-14 18:22 UTC

This package is not auto-updated.

Last update: 2024-09-23 14:59:59 UTC


README

KoolRouter是一个快速、易于集成的路由器。它受到了klein.php和[AltoRouter] (https://github.com/dannyvankooten/AltoRouter)的启发。

它应与分发器和HTTP接口一起使用,因为它(有意)不提供这些功能。

主要功能

  • 将URL路由到回调函数
  • 反向路由将路由和参数路由到对应的路径
  • 简单的路由语法

开始使用KoolRouter

  1. 确保已安装PHP 5.3.*
  2. 使用Composer或手动安装KoolRouter
  3. 让KoolRouter处理您的请求(例如通过URL重写)

支持的路由

您可以在路由中使用以下语法。

    *                    // 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 examples

    /pages[/index]?            // Matches "/pages" and "/pages/index"
    /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

示例

use ElzeKool\KoolRouter\Router;

$router = new Router();

// Setup route
$router->map(
    'GET /pages/[i:id]', 
    function($method, $path, $parameters, $extra) { 
        echo 'Hello i\'m page ' . $parameters['id'];
    },
    'page_view'
);

// Start routing
$router->run();


// Reverse routing
// (returns: /pages/10)
$route = $router->reverse('page_view', array(
    'id' => 10
));

Composer

  • 获取 Composer
  • elzekool/koolrouter 添加到您的 composer.json 文件中
{
    "require": {
        "elzekool/koolrouter": "0.*"
    }
}
  • 使用 php composer.phar install 安装/更新您的依赖项

许可证

(MIT许可证)

版权(c)2013 Elze Kool info@kooldevelopment.nl

特此授予任何获得此软件及其相关文档文件(“软件”)副本的任何人免费处理软件的权利,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本的权利,并允许软件提供者使用该软件,前提是遵守以下条件

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

软件按“原样”提供,不包括任何明示或暗示的保证,包括但不限于适销性、针对特定目的的适用性和非侵权性保证。在任何情况下,作者或版权所有者不对任何索赔、损害或其他责任负责,无论此类索赔、损害或其他责任是基于合同、侵权或其他方式,源于、因之或与此软件或软件的使用或其他交易有关。