简体   繁体   English

在前缀根路径中找不到路由

[英]Route Not Found In Prefix Root Path

I'm trying to setting up symfony/routing component on my project..我正在尝试在我的项目中设置 symfony/路由组件..

Things go well but when I define prefix for routes, it throw route not found exception for root path of this prefix.事情 go 很好,但是当我为路由定义前缀时,它会为该前缀的根路径抛出 route not found 异常。

For example, let assume I have bunch of admin routes.例如,假设我有一堆管理路由。 Instead of defining "admin" keyword on each route I made a prefix route all of those.我没有在每条路由上定义“admin”关键字,而是为所有这些设置了前缀路由。 So my dashboard path turned into "/" from "/admin" .所以我的仪表板路径从"/admin"变成了"/" " 。 And now it throws route not found error..现在它抛出找不到路由错误..

When I checked the route collections.当我检查路线 collections 时。 Dashboard path seems as "/admin/" .仪表板路径似乎为"/admin/" And its not matching with REQUEST_URI它与REQUEST_URI不匹配

Am I setting up the component poorly or there are some cautions that I need to do?我是否设置了组件不好,或者我需要做一些注意事项?

Here is part of RouteProvider这是 RouteProvider 的一部分

foreach (scanDirectory(ROOT_PATH . "/routes") as $file) {
    $subCollection = new RouteCollection();
    $filepath = ROOT_PATH . "/routes/" . $file;
    $routes = Yaml::parseFile($filepath);

    $prefix = "api";

    if (array_key_exists("prefix", $routes)){
        $prefix =  $routes["prefix"];
        unset($routes["prefix"]);
    }

    foreach ($routes as $name => $route) {
        $parameters = (new RouteParser($route))->parse();
        $subCollection->add(
            $name,
            new Route(...$parameters)
        );
    }
    $subCollection->addPrefix($prefix);
    $subCollection->addOptions([
        "trailing_slash_on_root" => false
    ]);
    $collection->addCollection($subCollection);
 }

I poked around a bit in the router component.我在路由器组件中戳了一下。 The trailing_slash_on_root functionality is implemented as part of the loader process. trailing_slash_on_root 功能是作为加载程序的一部分实现的。 So I think you need to set it in your routes file.所以我认为你需要在你的路由文件中设置它。 You did not provide an example of what your admin route files look like so I'm not positive.您没有提供管理路由文件的示例,所以我不肯定。 Normally I would expect to see only a master routes file loaded which in turn would load individual sets of routes such as your admin routes.通常,我希望只看到加载的主路由文件,该文件又会加载单独的路由集,例如您的管理路由。

However, using your posted code as an example, we can implement the same process that trailing_slash_on_root uses.但是,以您发布的代码为例,我们可以实现 trailing_slash_on_root 使用的相同过程。 Basically we explicitly drop the trailing slash for the dashboard route after all the processing takes place.基本上,在所有处理发生后,我们显式删除仪表板路由的尾部斜杠。 Here is a complete standalone working example taken mostly from the routing component docs:这是一个完整的独立工作示例,主要取自路由组件文档:

$rootCollection = new RouteCollection();
$adminCollection = new RouteCollection();

$route = new Route('/users',['_controller' => 'admin_users_controller']);
$adminCollection->add('admin_users',$route);

$route = new Route('/',['_controller' => 'admin_dashboard_controller']);
$adminCollection->add('admin_dashboard',$route);

$adminCollection->addPrefix('/admin'); # This actually adds the prefix

# *** Explicitly tweak the processed dashboard route ***
$route = $adminCollection->get('admin_dashboard');
$route->setPath('/admin');

$rootCollection->addCollection($adminCollection);

$context = new RequestContext('/');

// Routing can match routes with incoming requests
$matcher = new UrlMatcher($rootCollection, $context);
$parameters = $matcher->match('/admin/users');
var_dump($parameters);

$parameters = $matcher->match('/admin');
var_dump($parameters);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM