siro/routify

基于 Express 路由器的基础路由器

dev-master 2016-08-10 08:55 UTC

This package is auto-updated.

Last update: 2024-09-12 08:15:40 UTC


README

Build Status Scrutinizer Code Quality

一个受 Express 框架启发的简单 PHP 路由器。

Routify 是一个快速灵活的 PHP 5.4 及以上版本的路由器。

  • 灵活的正则表达式路由(受 Express 启发)
  • 具体用途。专注于做好一件事
  • 易于学习。简单的 API,让你想起 Express 或 Slim

入门指南

  1. 需要 PHP 5.4.x
  2. 使用 Composer(推荐)或手动安装 Routify
  3. 设置 URL 重写,以便所有请求都通过 index.php 处理,例如,使用 .htaccess 文件

示例

require 'vendor/autoload.php';

$router = new Routify\Router();

$middleware1 = function() {
    echo "middleware 1";
};

$middleware2 = function() {
    echo "middleware 2";
};

$router->get('/', function() {
        echo "This is an action";
    },
    ['before' => $middleware1, 'after' => $middleware2]
);

$router->get('/post/:slug/:id', function($slug, $id) {
	echo "You are seeing the post nº $id, with title: $slug";
});

$router->post('/new', function() {
	// something for the POST /new
});

$router->put('/', function() {
	// something for the PUT /
});

$router->delete('/:id', function($id) {
	// something for the DELETE /:id
});

$router->both('/hello/world', function() {
    // something for GET and POST requests
}, ['GET', 'POST']);

$router->any('/bye', function() {
    // something for any request method
}, ['before' => $middleware1, 'after' => $middleware2]);

// regular expression route
$router->get('/(login|logout), function() {
    // response for the login or logout route requested
});

$router->run();

测试并提交代码

新功能或修改必须在 pull requests 新代码之前用 PHPUnit 进行测试。