自定义一个简单的路由
接秒杀系列01上文
我们自定义了一个路由,http://192.168.0.101:8001/test 会进入到上图红色框中执行,页面显示 test …
使用第三方库
这里我们使用 FastRoute 库,来实现路由。 https://github/com/nikic/FastRoute,readme中有使用帮助。
1、安装
composer require nikic/fast-route
2、使用这个库,完成简单的路由示例
<?php
require_once __DIR__."/vendor/autoload.php";
use Swoole\Http\Request;
use Swoole\Http\Response;
$dispatcher = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $r) {
$r->addRoute('GET', '/test', function ($req) { // 添加一个路由
return $req->server['request_method'] . ', ' . $req->server['request_uri'];
});
});
$http = new Swoole\Http\Server("0.0.0.0", 80);
$http->on('request', function (Request $request, Response $response) use($dispatcher) {
$routeInfo = $dispatcher->dispatch($request->server['request_method'], $request->server['request_uri']); // [1, $handler, $var]
switch ($routeInfo[0]) {
case FastRoute\Dispatcher::NOT_FOUND:
$response->status(404);
$response->end();
break;
case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
$response->status(405);
$response->end();
break;
case FastRoute\Dispatcher::FOUND:
var_dump($routeInfo);
$handler = $routeInfo[1]; // $r->addRoute() 中的函数名,或者匿名函数
$vars = $routeInfo[2];
$response->end($handler($request));
break;
}
});
$http->start();
简单封装Request
Request.php 代码如下:
<?php
namespace App\core;
class Request {
protected $server = [];
protected $uri;
protected $queryParams;
protected $postParams;
protected $method;
protected $header=[];
protected $body;
protected $swooleRequest;
public function __construct(array $server, $uri, $queryParams, $postParams, $method, array $header, $body)
{
$this->server = $server;
$this->uri = $uri;
$this->queryParams = $queryParams;
$this->postParams = $postParams;
$this->method = $method;
$this->header = $header;
$this->body = $body;
}
public static function init(\Swoole\Http\Request $swooleRequest){
$server = $swooleRequest->server;
$method = $swooleRequest->server['request_method'] ?? 'GET';
$uri = $server['request_uri'];
$body = $swooleRequest->rawContent();
//关键点
$request = new self($server, $uri, $swooleRequest->get, $swooleRequest->post, $method, $swooleRequest->header, $body);
$request->swooleRequest = $swooleRequest;
return $request;
}
public function getServer(): array {
return $this->server;
}
public function setServer(array $server)
{
$this->server = $server;
}
public function getUri()
{
return $this->uri;
}
public function setUri($uri)
{
$this->uri = $uri;
}
public function getQueryParams()
{
return $this->queryParams;
}
public function setQueryParams($queryParams)
{
$this->queryParams = $queryParams;
}
public function getPostParams()
{
return $this->postParams;
}
public function setPostParams($postParams)
{
$this->postParams = $postParams;
}
public function getMethod()
{
return $this->method;
}
public function setMethod($method)
{
$this->method = $method;
}
public function getHeader(): array
{
return $this->header;
}
public function setHeader(array $header)
{
$this->header = $header;
}
public function getBody()
{
return $this->body;
}
public function setBody($body)
{
$this->body = $body;
}
public function getSwooleRequest()
{
return $this->swooleRequest;
}
public function setSwooleRequest($swooleRequest)
{
$this->swooleRequest = $swooleRequest;
}
}
index.php 代码如下:
<?php
require_once __DIR__."/vendor/autoload.php";
use Swoole\Http\Request;
use Swoole\Http\Response;
$dispatcher = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $r) {
$r->addRoute('GET', '/test', function ($req) { // 添加一个路由
return $req->getMethod() . ', ' . $req->getUri();
});
});
$http = new Swoole\Http\Server("0.0.0.0", 80);
$http->on('request', function (Request $request, Response $response) use($dispatcher) {
$myRequest = App\core\Request::init($request);
$routeInfo = $dispatcher->dispatch($myRequest->getMethod(), $myRequest->getUri()); // [1, $handler, $var]
switch ($routeInfo[0]) {
case FastRoute\Dispatcher::NOT_FOUND:
$response->status(404);
$response->end();
break;
case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
$response->status(405);
$response->end();
break;
case FastRoute\Dispatcher::FOUND:
$handler = $routeInfo[1]; // $r->addRoute() 中的函数名,或者匿名函数
$vars = $routeInfo[2]; // 附带的参数
$response->end($handler($myRequest));
break;
}
});
$http->start();