路由的功能就是分发请求到不一样的控制器,基于的原理就是正则匹配。接下来呢,咱们实现一个简单的路由器,实现的能力是php
[ 'id' => '23' ]
对于注册的路由,须要分红两类(下文提到的$uri是指$_SERVER['REQUEST_URI']去掉查询字符串的值)nginx
其实这是很明显的,由于静态的路由的话,咱们只须要和$uri直接比较相等与否就好了,而对于带参数路由,譬如/user/{uid},咱们须要在注册的时候,提取占位符名,把{**}这一部分替换为([a-zA-Z0-9_]+)这样的正则字符串,使用()是由于要作分组捕获,把占位符对应的值要取出来。
Dispatcher.php中有两个数组正则表达式
分别对应静态路由和带参数路由,另外要注意,这两个数组是二维数组,第一维存储http method,第二维用来存储正则字符串(静态路由自身就是这个值,而带参数路由是把占位符替换后的值),最后的value是一个Route对象json
这个类很好理解,用来存储注册路由的一些信息数组
固然,这个类还能够多存储一点信息,譬如带参数路由最原始的字符串(/user/{uid}),这里简单作了composer
第一步很简单,主要说明第二步
对于三个独立的正则字符串(定界符是~):函数
~^/user/([^/]+)/(\d+)$~ ~^/user/(\d+)$~ ~^/user/([^/]+)$~
咱们能够合起来,获得post
~^(?: /user/([^/]+)/(\d+) | /user/(\d+) | /user/([^/]+) )$~x
?:是非捕获型分组
这个转化很简单,咱们怎么知道那个正则被匹配了呢??
举个例子:ui
preg_match($regex, '/user/nikic', $matches); => [ "/user/nikic", # 彻底匹配 "", "", # 第一个(空) "", # 第二个(空) "nikic", # 第三个(被使用) ]
能够看到,第一个非空的位置就能够推断出哪一个路由被匹配了(第一个彻底匹配要剔除),咱们须要一个数组要映射它this
[ 1 => ['handler0', ['name', 'id']], 3 => ['handler1', ['id']], 4 => ['handler2', ['name']], ]
1是由于排除第一个匹配
3是由于第一个路由有两个占位符
4是由于第二个路由有一个占位符
上面的数组,咱们能够注册的methodToRegexToRoutesMap这个量造成的,我是这么写的
$regexes = array_keys($this->methodToRegexToRoutesMap[$httpMethod]); foreach ($regexes as $regex) { $routeLookup[$index] = [ $this->methodToRegexToRoutesMap[$httpMethod][$regex]->handler, $this->methodToRegexToRoutesMap[$httpMethod][$regex]->variables, ]; $index += count($this->methodToRegexToRoutesMap[$httpMethod][$regex]->variables); }
调用回调函数,返回一个数组,第一个值用来判断最终有没有找到
<?php namespace SalamanderRoute; class Route { /** @var string */ public $httpMethod; /** @var string */ public $regex; /** @var array */ public $variables; /** @var mixed */ public $handler; /** * Constructs a route (value object). * * @param string $httpMethod * @param mixed $handler * @param string $regex * @param array $variables */ public function __construct($httpMethod, $handler, $regex, $variables) { $this->httpMethod = $httpMethod; $this->handler = $handler; $this->regex = $regex; $this->variables = $variables; } /** * Tests whether this route matches the given string. * * @param string $str * * @return bool */ public function matches($str) { $regex = '~^' . $this->regex . '$~'; return (bool) preg_match($regex, $str); } }
<?php /** * User: salamander * Date: 2017/11/12 * Time: 13:43 */ namespace SalamanderRoute; class Dispatcher { /** @var mixed[][] */ protected $staticRoutes = []; /** @var Route[][] */ private $methodToRegexToRoutesMap = []; const NOT_FOUND = 0; const FOUND = 1; const METHOD_NOT_ALLOWED = 2; /** * 提取占位符 * @param $route * @return array */ private function parse($route) { $regex = '~^(?:/[a-zA-Z0-9_]*|/\{([a-zA-Z0-9_]+?)\})+/?$~'; if(preg_match($regex, $route, $matches)) { // 区分静态路由和动态路由 if(count($matches) > 1) { preg_match_all('~\{([a-zA-Z0-9_]+?)\}~', $route, $matchesVariables); return [ preg_replace('~{[a-zA-Z0-9_]+?}~', '([a-zA-Z0-9_]+)', $route), $matchesVariables[1], ]; } else { return [ $route, [], ]; } } throw new \LogicException('register route failed, pattern is illegal'); } /** * 注册路由 * @param $httpMethod string | string[] * @param $route * @param $handler */ public function addRoute($httpMethod, $route, $handler) { $routeData = $this->parse($route); foreach ((array) $httpMethod as $method) { if ($this->isStaticRoute($routeData)) { $this->addStaticRoute($httpMethod, $routeData, $handler); } else { $this->addVariableRoute($httpMethod, $routeData, $handler); } } } private function isStaticRoute($routeData) { return count($routeData[1]) === 0; } private function addStaticRoute($httpMethod, $routeData, $handler) { $routeStr = $routeData[0]; if (isset($this->staticRoutes[$httpMethod][$routeStr])) { throw new \LogicException(sprintf( 'Cannot register two routes matching "%s" for method "%s"', $routeStr, $httpMethod )); } if (isset($this->methodToRegexToRoutesMap[$httpMethod])) { foreach ($this->methodToRegexToRoutesMap[$httpMethod] as $route) { if ($route->matches($routeStr)) { throw new \LogicException(sprintf( 'Static route "%s" is shadowed by previously defined variable route "%s" for method "%s"', $routeStr, $route->regex, $httpMethod )); } } } $this->staticRoutes[$httpMethod][$routeStr] = $handler; } private function addVariableRoute($httpMethod, $routeData, $handler) { list($regex, $variables) = $routeData; if (isset($this->methodToRegexToRoutesMap[$httpMethod][$regex])) { throw new \LogicException(sprintf( 'Cannot register two routes matching "%s" for method "%s"', $regex, $httpMethod )); } $this->methodToRegexToRoutesMap[$httpMethod][$regex] = new Route( $httpMethod, $handler, $regex, $variables ); } public function get($route, $handler) { $this->addRoute('GET', $route, $handler); } public function post($route, $handler) { $this->addRoute('POST', $route, $handler); } public function put($route, $handler) { $this->addRoute('PUT', $route, $handler); } public function delete($route, $handler) { $this->addRoute('DELETE', $route, $handler); } public function patch($route, $handler) { $this->addRoute('PATCH', $route, $handler); } public function head($route, $handler) { $this->addRoute('HEAD', $route, $handler); } /** * 分发 * @param $httpMethod * @param $uri */ public function dispatch($httpMethod, $uri) { $staticRoutes = array_keys($this->staticRoutes[$httpMethod]); foreach ($staticRoutes as $staticRoute) { if($staticRoute === $uri) { return [self::FOUND, $this->staticRoutes[$httpMethod][$staticRoute], []]; } } $routeLookup = []; $index = 1; $regexes = array_keys($this->methodToRegexToRoutesMap[$httpMethod]); foreach ($regexes as $regex) { $routeLookup[$index] = [ $this->methodToRegexToRoutesMap[$httpMethod][$regex]->handler, $this->methodToRegexToRoutesMap[$httpMethod][$regex]->variables, ]; $index += count($this->methodToRegexToRoutesMap[$httpMethod][$regex]->variables); } $regexCombined = '~^(?:' . implode('|', $regexes) . ')$~'; if(!preg_match($regexCombined, $uri, $matches)) { return [self::NOT_FOUND]; } for ($i = 1; '' === $matches[$i]; ++$i); list($handler, $varNames) = $routeLookup[$i]; $vars = []; foreach ($varNames as $varName) { $vars[$varName] = $matches[$i++]; } return [self::FOUND, $handler, $vars]; } }
location / { try_files $uri $uri/ /index.php$is_args$args; # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
{ "name": "salmander/route", "require": {}, "autoload": { "psr-4": { "SalamanderRoute\\": "SalamanderRoute/" } } }
<?php include_once 'vendor/autoload.php'; use SalamanderRoute\Dispatcher; $dispatcher = new Dispatcher(); $dispatcher->get('/', function () { echo 'hello world'; }); $dispatcher->get('/user/{id}', function ($args) { echo "user {$args['id']} visit"; }); // Fetch method and URI from somewhere $httpMethod = $_SERVER['REQUEST_METHOD']; $uri = $_SERVER['REQUEST_URI']; // 去掉查询字符串 if (false !== $pos = strpos($uri, '?')) { $uri = substr($uri, 0, $pos); } $routeInfo = $dispatcher->dispatch($httpMethod, $uri); switch ($routeInfo[0]) { case Dispatcher::NOT_FOUND: echo '404 not found'; break; case Dispatcher::FOUND: $handler = $routeInfo[1]; $vars = $routeInfo[2]; $handler($vars); break; }