此包已被废弃且不再维护。没有建议替代包。

PHP 路由器。

v2.2.0 2018-03-07 22:40 UTC

This package is not auto-updated.

Last update: 2022-04-16 07:32:47 UTC


README

PHP version Build Status Coverage Status

Quest 是一个 PHP >=7 的路由器。

名称

特别是,各种英雄都在寻找传说中的戴德里克神器,以获得强大的战斗力和魔法效益。

—— 古特斯福德的神德鲁斯,现代异端

入门

安装

Quest 通过 Composer & Packagist 提供。

{
  "require": {
    "oscarpalmer/quest": "2.2.*",
    "oscarpalmer/shelf": "2.3.*"
  }
}

Quest 使用 Shelf,这是一个由我制作的类似 Rack 的 PHP 接口,非常酷。

基本用法

use oscarpalmer\Quest\Quest;

$quest = new Quest;

$quest->get("/", function () {
    return "Hello, world!";
});

$quest->run();

就这么多!但我相信你肯定还想了解关于 路由API 的更多信息。

路由

如果你曾经使用过类似 Sinatra 的东西,那么在 Quest 中进行路由不应该太难。

有三种路由参数可以使处理复杂路由变得易如反掌。

  • *: 匹配几乎所有内容;即 .*?
  • :param: 匹配任何单词字符;即 \w+
  • (anything): 对 anything 的可选匹配;即 (anything)?

路由示例

# the following route will match /a/simple/path
$quest->get("/a/simple/path", $callback);

# the following route will match
# /path/to/dir/file.extension and /a/b/c/d/e/f/b/g/h.i
$quest->get("/*/:file.:ext", $callback);

# optional parameters should be wrapped in parentheses;
# the following route will therefore match both
# /path/to/dir/file.extension and /path/to/dir/file
$quest->get("/*/:file(.:ext)", $callback);

回调函数

捕获并刷新回声。请使用返回语句输出内容。

错误处理器

当您或用户遇到问题时,有一个错误处理器总是很有用。特定状态的处理程序优于通配符处理程序,通配符处理程序又优于默认处理程序。

# Calling handlers
$quest->error();    # The error-handler defaults to 500.
$quest->error(401); # You can choose the error, too.

# Defining handlers
$quest->error(401, function () {}); # Status-specific errors are possible, too.

服务器相关内容

现在,如果您的服务器没有正确设置,Quest 就不能做很多事情,因此这里有两个使用 Apachenginx 进行“正确”URL重写的小片段。

Apache

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]

nginx

try_files $uri /index.php;

API

# Constructor
$quest = new Quest($a, $b, $c);     #  Optional parameters; array of routes, and Request
                                    #  and Response objects from Shelf; useful for testing

# Constants and properties
$quest::VERSION;                    #  Current Quest version number
$quest->errors;                     #  Array of error callbacks with status codes as keys
$quest->filters;                    #  Array of filters with at most two children; "after" and "before"
$quest->parameters;                 #  Object of route parameters
$quest->request;                    #  Shelf Request object
  $quest->cookies;                  #    $_COOKIE object belonging to the Request object
  $quest->data;                     #    $_POST object
  $quest->files;                    #    $_FILES object
  $quest->query;                    #    $_GET object
  $quest->server;                   #    $_SERVER object
  $quest->session;                  #    $_SESSION object
$quest->response;                   #  Shelf Response object
$quest->routes;                     #  Array of routes

# Filter methods
$quest->after($path, $callback);    #  Add an after filter with a path to run after routing
$quest->before($path, $callback);   #  Add a before filter with a path to run before routing
                                    #  $path must be a string, and $callback must be a callable

# Route methods
$quest->delete($path, $callback);   #  Add a DELETE route;
$quest->get($path, $callback);      #  Add a GET (and HEAD) route
$quest->post($path, $callback);     #  Add a POST route
$quest->put($path, $callback);      #  Add a PUT route
                                    #  $path must be a string and $callback must be a callable

# Error method
$quest->error($status, $callback);  #  Add or run an error callback; will run an already defined
                                    #  or default callback if no $callback is supplied
                                    #  $status can be a valid status code or null (500 error);
                                    #  $callback must be a callable if supplied

# Helper methods
$quest->contentType($optional);     #  Get or set the content type of the response
$quest->header($name, $optional);   #  Get or set a response header

# Run method
$quest->run();                      #  Let the questing (routing) begin!
                                    #  Will run filters and the first matching route's callback

许可证

MIT 许可;有关更多信息,请参阅 许可证文件