easyswoole
easyswoole copied to clipboard
路由设置setRouterNotFoundCallBack未找到路由匹配之后,框架正常访问路劲失效
Router.php 设置
$this->setPathInfoMode(false); $this->setGlobalMode(false); $this->setMethodNotAllowCallBack(function (Request $request,Response $response){ return '/index/index/index';//重定向到index路由 }); $this->setRouterNotFoundCallBack(function (Request $request,Response $response){ return '/index/index/index';//重定向到index路由 });
访问不存在路由会重定向至/index/index/index,访问正常路径http://localhost/admin/login/login 也会重定向至/index/index/index
你是不是强制开启了路由全局模式?
$this->setGlobalMode(false);//并没有开启强制路由
贴下abstract router代码谢谢
`<?php /**
- Created by PhpStorm.
- User: yf
- Date: 2018/7/29
- Time: 下午4:00 */
namespace EasySwoole\Http\AbstractInterface; use FastRoute\DataGenerator\GroupCountBased; use FastRoute\RouteCollector; use FastRoute\RouteParser\Std;
abstract class AbstractRouter { private $routeCollector; private $methodNotAllowCallBack = null; private $routerNotFoundCallBack = null; private $globalMode = false; private $pathInfoMode = true;
final function __construct()
{
$this->routeCollector = new RouteCollector(new Std(),new GroupCountBased());
$this->initialize($this->routeCollector);
}
abstract function initialize(RouteCollector $routeCollector);
/**
* @return bool
*/
public function isPathInfoMode(): bool
{
return $this->pathInfoMode;
}
/**
* @param bool $pathInfoMode
*/
public function setPathInfoMode(bool $pathInfoMode): void
{
$this->pathInfoMode = $pathInfoMode;
}
function getRouteCollector():RouteCollector
{
return $this->routeCollector;
}
function setMethodNotAllowCallBack(callable $call)
{
$this->methodNotAllowCallBack = $call;
}
function getMethodNotAllowCallBack()
{
return $this->methodNotAllowCallBack;
}
/**
* @return null
*/
public function getRouterNotFoundCallBack()
{
return $this->routerNotFoundCallBack;
}
/**
* @param null $routerNotFoundCallBack
*/
public function setRouterNotFoundCallBack($routerNotFoundCallBack): void
{
$this->routerNotFoundCallBack = $routerNotFoundCallBack;
}
/**
* @return bool
*/
public function isGlobalMode(): bool
{
return $this->globalMode;
}
/**
* @param bool $globalMode
* @return void
*/
public function setGlobalMode(bool $globalMode): void
{
$this->globalMode = $globalMode;
}
}`
表示有相同的问题
$this->setPathInfoMode(false); $this->setGlobalMode(false); $this->setMethodNotAllowCallBack(function (Request $request,Response $response){ return '/index/index/index';//重定向到index路由 }); $this->setRouterNotFoundCallBack(function (Request $request,Response $response){ return '/index/index/index';//重定向到index路由 });
你这个有2种选择, 方法一:取消路由 setRouterNotFoundCallBack 的回调设置。 方法二:在路由中注册 $routeCollector->get('/admin/login/login', '/test'); 这个路由。