简体   繁体   中英

Route Not Found In Prefix Root Path

I'm trying to setting up symfony/routing component on my project..

Things go well but when I define prefix for routes, it throw route not found exception for root path of this prefix.

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. So my dashboard path turned into "/" from "/admin" . And now it throws route not found error..

When I checked the route collections. Dashboard path seems as "/admin/" . And its not matching with REQUEST_URI

Am I setting up the component poorly or there are some cautions that I need to do?

Here is part of 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. 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. 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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