bafs / via
此包已被废弃,不再维护。没有建议的替代包。
Via 是一个简单且可扩展的路由器,受 expressjs 启发。
v1.0.1
2016-04-28 10:07 UTC
Requires
- php: >=5.3.0
Requires (Dev)
- bafs/testify: dev-master
- rmccue/requests: dev-master
This package is not auto-updated.
Last update: 2023-10-27 12:14:26 UTC
README
Via 是一个简单且可扩展的路由器,受 express 启发。
这是第一个版本,请谨慎使用!
所需
- PHP 5.3+
- (可选) URL 重写
- Composer.
使用 Composer 安装
composer require bafs/via dev-master
composer install
使用 Via
完整的 Hello World 示例
<?php use Via\Router; require 'vendor/autoload.php'; $app = new Router(); $app->get('', function($req, $res) { $res('Hello world !'); }); $app(); // run the router
简单路由
$app->get('/test', function($req, $res) { $res('Test OK'); // print 'Test OK' $res->send('Test OK'); // Same as above });
带有参数
$app->post('/test/:var', function($req, $res) { $res($req('var')); $res($req->params->var)); // Same as above });
多种方法
$app->on(['GET', 'POST'], '/test', function() { // ... });
匹配多个路由
// Match both (print '12') $app->get('/test', function($req, $res, $next) { $res('1'); $next(); // Continue to test next routes }); $app->get('/test', function($req, $res) { $res('2'); });
使用命名空间
$app->with('/sub', function($app) { $app->with('/a', function($app) { // inside /sub/a $app->get('/test', function($req, $res){ // match /sub/a/test }); }); });
使用基本 Via 引擎
$app->get('/test', function($req, $res, $next) { // You can use $title inside view.html $html = $res->render('view.html', ['title' => 'My Title']); $res->send($html); });
高级用法
Using
函数将自动调用 $next()
$app->using(function($req, $res) { $res->contentType('text/plain'); $res->set('X-Custom-Header', 'test'); // will continue to next route }); // ...
使用特定方法
class A { function b($req, $res) {...} } $app->get('/', 'A@b');
处理控制器
class MyController { function getUser($req, $res) {...} function postUser($req, $res) {...} } $app->get('/test/@@', 'MyController'); // GET /test/user will call "getUser" // POST /test/user will call "postUser"
使用正则表达式
// GET /reg/F00D will print "F00D" $app->get('R/reg/([0-9A-F]+)', function($req, $res, $next) { $res($req(0)); });
文档
路由器 (app)
get(string $route, function $callback)
添加带有 GET 请求动词的路由
post(string $route, function $callback)
添加带有 POST 请求动词的路由
put(string $route, function $callback)
添加带有 PUT 请求动词的路由
delete(string $route, function $callback)
添加带有 DELETE 请求动词的路由
on(string|array $verb, string $route, function $callback)
添加路由(可以使用多个动词)
all(function $callback)
添加始终匹配的路由
using(function $callback)
添加始终匹配并转到下一个的路由
with(string $namespace, function $callback)
添加命名空间(前缀所有路由)
get(string $name)
从容器中获取值
set(string $name, mixed $value)
在应用程序容器中设置值
render(string $view, array $data)
渲染视图
$res->render("user.html", ["title" => "User"]);
在模板中我们可以使用:<title><?=$title?></title>
请求
字段
$params
从URL映射的参数$query
从URL映射的查询$cookies
Cookie$body
POST中的属性$ip
获取远程IP地址$path
获取请求的URL路径$xhr
检查是否使用'XMLHttpRequest'执行请求$verb
HTTP动词(方法)$url
获取当前URL
get(string $field)
获取特定的头部信息
$req->header('host');
$req->header(...);
别名
param(string $name)
如果存在则返回(按顺序)$req->params->$name, $req->body->$name 或 $req->query->$name
$req->param('str');
$req(...);
别名
is(string $type)
检查MIME类型是否在头部中
$req->is('html');
[静态] getRelativeURIToRoot()
返回相对于根的路径(例如,对于视图中的资产很有用)
[静态] isRewriteActive()
检查重写引擎是否处于活动状态(重写可能已启用但未激活)
响应
send([int $status], string $message)
发送输出缓冲区
$res->send('text');
$res->send(404, 'Not found');
$res->send(new MyObj());
打印json$res(...);
是$res->send(...);
的别名
json([int $status], string $json)
发送输出缓冲区并强制JSON内容类型
$res->json("{ user: 'Mario' }");
ssend([int $status], string $message)
与 send 相同,但编码特殊HTML字符
redirect([int $status], string $url)
重定向客户端到指定URL
$res->redirect('http://cern.ch');
$res->redirect(301, '/test');
download(string $path, [string $filename, string $contentType])
强制下载指定文件
$res->download('myImage.png');
$res->download('CV_en_v4.pdf', 'Smith_CV.pdf');
file($filename, [$content])
读取或写入文件
$res->file('myImage.png');
读取文件$res->file('file.txt', $data);
写入数据
contentType(string $type)
$res->contentType('text/plain');
设置 'Content-Type' 报头为 text/plain
set(string $field, string $value)
设置报头参数
$res->set('Content-Type', 'text/plain');
设置 'Content-Type' 报头为 text/plain-
'Content-Type' => 'text/plain', 'ETag' => '10000' ]);```
status(int $code)
$res->status(404);
cookie(string $name, [string $value, array $options])
$res->cookie("TestCookie", "something", ["maxAge" => 3600, "secure" => true]);
clearCookie(string $name, [array $options])
$res->clearCookie("TestCookie");
清除 'TestCookie' 饼干
render(string $view, array $data)
在 "Router" 中 render 的别名
Dev
要运行测试,请转到测试文件夹,编辑URL常量(run.php第10行)并运行 php run.php
。