thinkphp/library/think/route/Rule.php
第794行:
protected function dispatch($request, $route, $option) { if ($route instanceof \Closure) { // 执行闭包 $result = new CallbackDispatch($request, $this, $route); } elseif ($route instanceof Response) { $result = new ResponseDispatch($request, $this, $route); } elseif (isset($option['view']) && false !== $option['view']) { $result = new ViewDispatch($request, $this, $route, is_array($option['view']) ? $option['view'] : []); } elseif (!empty($option['redirect']) || 0 === strpos($route, '/') || strpos($route, '://')) { // 路由到重定向地址 $result = new RedirectDispatch($request, $this, $route, [], isset($option['status']) ? $option['status'] : 301); } elseif (false !== strpos($route, '\\')) { // 路由到方法 $result = $this->dispatchMethod($request, $route); } elseif (0 === strpos($route, '@')) { // 路由到控制器 $result = $this->dispatchController($request, substr($route, 1)); } else { // 路由到模块/控制器/操作 $result = $this->dispatchModule($request, $route); } return $result; }
改成:
protected function dispatch($request, $route, $option) { if ($route instanceof \Closure) { // 执行闭包 $result = new CallbackDispatch($request, $this, $route); } elseif ($route instanceof Response) { $result = new ResponseDispatch($request, $this, $route); } elseif (is_array($route) && count($route) == 2){ $result = $this->dispatchMethod($request, $route[0] . '@' . $route[1]); } elseif (isset($option['view']) && false !== $option['view']) { $result = new ViewDispatch($request, $this, $route, is_array($option['view']) ? $option['view'] : []); } elseif (!empty($option['redirect']) || 0 === strpos($route, '/') || strpos($route, '://')) { // 路由到重定向地址 $result = new RedirectDispatch($request, $this, $route, [], isset($option['status']) ? $option['status'] : 301); } elseif (false !== strpos($route, '\\')) { // 路由到方法 $result = $this->dispatchMethod($request, $route); } elseif (0 === strpos($route, '@')) { // 路由到控制器 $result = $this->dispatchController($request, substr($route, 1)); } else { // 路由到模块/控制器/操作 $result = $this->dispatchModule($request, $route); } return $result; }
优点:路由可以这样定义:
Route::get('hello/:name', [\app\index\controller\Index::class, 'hello']);
,编辑器里可以观察到方法是否被引用:
标签:option,route,request,改进,result,elseif,ThinkPHP5.1,路由 From: https://www.cnblogs.com/felixwan/p/16997865.html