buuum/route

PHP 的快速、功能齐全的 RESTful 请求路由器

v2.0.1 2017-04-19 16:36 UTC

README

Build Status Packagist license

简单且极具灵活性的 PHP 路由器类,支持路由参数、RESTful、过滤器以及反向路由。

入门

使用 Buuum 需要 PHP >= 5.5。

安装

系统要求

使用 Buuum\Buuum 需要 PHP >= 5.5.0,但推荐使用最新的稳定版本 PHP。

Composer

Buuum 可在 Packagist 上找到,并可以使用 Composer 安装。

composer require buuum/route

手动

只要你的自动加载器遵循 PSR-0 或 PSR-4 标准,你就可以使用自己的自动加载器。只需将 src 目录的内容放入你的 vendor 目录中。

重写请求

要使用 Buuum,你需要将所有请求重写到单个文件。有多种方法可以实现,但这里提供了 Apache 和 Nginx 的示例。

Apache .htaccess

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ index.php [NC,L]

Nginx nginx.conf

server {
    listen 80;
    server_name mydevsite.dev;
    root /var/www/mydevsite/public;

    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

        # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
        fastcgi_intercept_errors on;
    }
}

映射你的路由

use Buuum\Router;

$router = new Router('/examples');

$router->filter('auth', function ($_requesturi) {
    //return 'hola auth';
    var_dump($_requesturi);
    return [
        'passed' => true,
        response => [
            'uri' => $_requesturi
        ]
    ];
    
    return [
        'passed' => false,
        'response' => new RedirectResponse('path')
    ];
});

$router->get('/', function () {
    return 'hello world';
});

$router->get('/part/', function () {
    return 'hello world part';
})->setName('part');

$router->get('/part/{id:[0-9]+}/', function ($id) {
    return 'hello world part' . $id;
})->setName('partid');

$router->group(['before' => ['auth']], function (\Buuum\Router $router) {
    $router->get('/testbefore/', function () {
        return 'hello test before';
    });
});

$router->group(['prefix' => 'en'], function (\Buuum\Router $router) {

    $router->get('/', function () {
        return 'home with prefix en';
    })->setName('home');

});

$router->group(['prefix' => 'es'], function (\Buuum\Router $router) {

    $router->get('/', function () {
        return 'home with prefix es';
    })->setName('home');

    $router->group(['prefix' => 'admin'], function (Router $router) {
        $router->get('/', function () {
            return 'hola 2 prefix';
        })->setName('prefix2');
    });

});

$router->get('/home/', function () {
    return 'home without prefix';
})->setName('home');

多个路由一个控制器

$router->get(['/home/','/en/home/','/fr/home/'], function () {
    return 'home';
})->setName('home');

多个方法一个控制器

$router->map(['GET','POST'], '/home/', function () {
    return 'home';
})->setName('home');

匹配请求

$data = $router->getData();
$dispatcher = new \Buuum\Dispatcher($data);

$request = \Symfony\Component\HttpFoundation\Request::createFromGlobals();

try {
    echo $dispatcher->dispatchRequest($request->getMethod(), $request->getUri());
} catch (\Buuum\Exception\HttpRouteNotFoundException $e) {
    echo 'route not found';
} catch (\Buuum\Exception\HttpMethodNotAllowedException $e) {
    echo 'not method allowed';
} catch (\Exception $e) {
    echo 'error 500';
}

URL 分发

$data = $router->getData();
$dispatcher = new \Buuum\Dispatcher($data);

$request = \Symfony\Component\HttpFoundation\Request::createFromGlobals();

echo $dispatcher->getUrlRequest('home', [], $request->getUri());
echo "\n";
echo $dispatcher->getUrlRequest('prefix2', [], $request->getUri());
echo "\n";
echo $dispatcher->getUrlRequest('partid', ['id' => 555], $request->getUri();
echo "\n";

待办事项

- add requestinterface optional (guzzle / psr7)
- default pregmatches
- add cache route collection
- add cache dispatcher

许可证

MIT 许可证 (MIT)

版权所有 (c) 2016 alfonsmartinez

特此授予任何人免费获得本软件及其相关文档文件(以下简称“软件”)副本的权利,无限制地处理该软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件的副本,并允许向提供软件的人员授权其这样做,但受以下条件约束:

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

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