easyswoole icon indicating copy to clipboard operation
easyswoole copied to clipboard

路由设置setRouterNotFoundCallBack未找到路由匹配之后,框架正常访问路劲失效

Open fuheng1993 opened this issue 4 years ago • 5 comments

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

fuheng1993 avatar Jun 02 '20 07:06 fuheng1993

你是不是强制开启了路由全局模式?

kiss291323003 avatar Jun 02 '20 15:06 kiss291323003

$this->setGlobalMode(false);//并没有开启强制路由

fuheng1993 avatar Jun 02 '20 16:06 fuheng1993

贴下abstract router代码谢谢

kiss291323003 avatar Jun 02 '20 16:06 kiss291323003

`<?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;
}

}`

fuheng1993 avatar Jun 02 '20 16:06 fuheng1993

表示有相同的问题

guihouchang avatar Aug 07 '20 18:08 guihouchang

$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'); 这个路由。

XueSiLf avatar Mar 23 '24 14:03 XueSiLf