justim/vice

Vice 是一个用于轻松调度给定 URL 行为的小型网络框架

dev-master 2015-03-11 17:13 UTC

This package is auto-updated.

Last update: 2024-09-05 04:15:08 UTC


README

Vice 是一个用于轻松调度给定 URL 行为的小型网络框架。

它使用了一些巧妙的技巧,确保您不必手动进行操作;

  • 轻松限制 HTTP 方法
  • 简单的 AJAX 响应
  • 根据名称填充参数,易于访问
  • URI 中的命名参数
  • 强大的过滤器

要求

  • PHP >= 5.4

安装

  • 为了使 Vice 运作,您只需要 Vice.php 文件,下载它并进行修改
  • 还可在 Packagist(Composer)中找到

示例应用程序

您可以通过在此存储库中的 example 目录查看所有操作。您可以使用 PHP 内置服务器运行示例

php -S localhost:9000 -t example/

现在在浏览器中打开 https://:9000 来查看示例。

示例

基本示例

$app = new Vice;
$app->route('/', function()
{
    echo 'Hello world!';
});
$app->run();

JSON 助手

$app = new Vice;

// route that only responds to ajax-post requests
$app->post('/users', 'is:ajax', function($json)
{
    // builtin helper, sets a header, encodes it, and dies
    $json([
        [
            'name' => 'Pizza kat',
        ]
    ]);
});

全局存储

$app = new Vice('/', [
    'db' => new Database, // some database connection
]);

$app->get('/', function($db)
{
    // $db is the one that you put in the store
});

高级过滤器

$app = new Vice;
$app->registerFilter('is:logged', function()
{
    if (/* some login check */)
    {
        return [
            'name' => 'Flavored mushrooms',
        ];
    }
    else
    {
        return false;
    }
});

$app->get('/admin', 'is:logged', function($isLogged)
{
    // $isLogged would contain the result of the filter,
	// if the filter fails, then this function is never called
	echo 'Hello ' . $isLogged['name'] . '!';
});

注意:过滤器在每次调用时都会运行。

子应用程序

$app = new Vice('/', [ 'db' => new Database ]);
$users = new Vice;

$users->get('<id>', function($json, $db, $id, $param)
{
    // $param('id') === $id
    $json($db->users->get($id));
});

// users subapp is available at /users and only for ajax-get-requests
// this would make the route /users/1 available
$app->get('users', 'is:ajax', $users);

子应用程序与其父应用程序共享过滤器存储,但反之亦然。

链式多个过滤器

$app = new Vice('/', [ 'currentUser' => 1, 'db' => new Database ]);

$app->registerFilter('is:logged', function($db, $currentUser)
{
    // check for existince of current user and return it
    return $db->users->get($currentUser) ?: false;
});

$app->registerFilter('is:admin', function($isLogged)
{
    return $isLogged['admin'] === true;
});

$app->route('admin', 'is:logged is:admin', function($isLogged)
{
    echo 'Hello ' . $isLogged['name'] . ', you are an admin!';
});

// a different way to handle this is by making a filter dependable
// on another filter. this way you don't have to specify 'is:logged'
// in your route definition
$app->registerFilter('is:admin', 'is:logged', function($isLogged)
{
	return $isLogged['admin'] === true;
});

$app->route('admin', 'is:admin', function($isLogged)
{
	// $isLogged is still available, even though it was not specified for this particular route
    echo 'Hello ' . $isLogged['name'] . ', you are an admin!';
});

内置过滤器

  • GET 请求
  • POST 请求
  • PUT 请求
  • DELETE 请求
  • AJAX 请求

(通过 $app->get(..) 或作为过滤器 $app->route('/', 'is:get')(所有其他操作相同)

内置命名参数

  • $post -> function($key, $default = null) - 对 $_POST 的包装
  • $get -> function($key, $default = null) - 对 $_GET 的包装
  • $param -> function($key, $default = null) - 对 URI 中的参数的包装
  • $server -> function($key, $default = null) - 对 $_SERVER 的包装
  • $store -> function($key, $default = null) - 对全局存储的包装
  • $filter -> function($key, $default = null) - 对传递的过滤器结果的包装
  • $ajax -> boolean,表示请求是否为 AJAX 请求
  • $json -> function($data) JSON 助手
  • paramsstorefilterResults(按此顺序)获取键

一些技术见解

  • 每个路由都编译为正则表达式,然后与当前 URI 进行匹配。当找到匹配项时,将测试该路由的所有定义的过滤器。如果仍然匹配,则运行相应的操作。当操作本身是一个应用程序时,整个过程再次运行,但不需要我们已匹配的前缀。经过一些循环,可能发生了某些事情,我们完成了。
  • 使用 Reflection 确定参数的值
  • 这是一个单独的类,其余的都是函数

待办事项

  • 一些适当的错误处理