承接 plugowski/php_router 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

plugowski/php_router

最新稳定版本:1.0

Composer 安装命令:

composer require plugowski/php_router

包简介

Simple routing engine, which allows anonymous functions to execute code on route.

README 文档

README

PHP router inspired on FatFree, CakePHP and own invention.

It allows to use anonymous functions as callback as well as OOP classes and methods. In addition, it detects if request is asynchronous or not (defined by [ajax] tag in rute definition).

Installation

Just clone that repository or use composer:

composer require plugowski/php_router

Next in your project create .htaccess file which point on your dispatcher file:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule .* index.php?url=$1 [QSA,L]
</IfModule>

Usage

Basic usage looks like code below:

<?php
require __DIR__ . '/vendor/autoload.php';

use PhpRouter\Route;
use PhpRouter\RouteCollection;
use PhpRouter\Router;
use PhpRouter\RouteRequest;

$routing = new RouteCollection();
$routing->attach(new Route('GET /', function(){
    echo 'Hello World';
}));

(new Router(new RouteRequest(), $routing))->run();

If you want to trigger OOP classes and methods, just write full namespace and method reference (-> or ::):

new Route('GET /', '\Full\Namespace\Class->method');

For named parameters use @name tag, for example:

new Route('GET /order/@id', '\Full\Namespace\Class->method');

Standard all named parameters will be recognized as (\w-)+, but if you want to define your own pattern, do it in second argument as associative array, where keys match to @ tag:

// $_SERVER['REQUEST_URI'] = '/order/14-XA-43321'
new Route('GET /order/@id', ['id' => '\d{2}\-\w{2}\-\d{5}'], function($params) {
  echo 'Order ID: ' . $params['id']; // Order ID: 14-XA-43321 
});

All named params are passed into called function as associative array in first argument.

Licence

New BSD Licence: https://opensource.org/licenses/BSD-3-Clause

统计信息

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

GitHub 信息

  • Stars: 3
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: New
  • 更新时间: 2016-02-15