0

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);
 }
Teoman Tıngır
  • 2,766
  • 2
  • 21
  • 41
  • So /admin/ is the only route giving your problems? It is possible you need to configure [trailing_slash_on_root](https://symfony.com/blog/new-in-symfony-4-1-configurable-trailing-slash-on-imported-routes) to false. If the link does not help then consider updating your question to show how you are configuring your routes. – Cerad Nov 07 '19 at 13:41
  • @Cerad I added it as option to route collection but nothing has changed. – Teoman Tıngır Nov 07 '19 at 14:17
  • 1
    Again, you really need to show a bit of code. There are a number of ways to configure routes and to match them. And I would not be surprised if writing a minimal test case reveals the solution. – Cerad Nov 07 '19 at 14:38
  • @Cerad Sorry about the question. I didn't share any code earlier because I think I'm missing something. And If I explain my problem well, there wont be need the code.. Now I'm updated my question and share some part of my route provider. – Teoman Tıngır Nov 07 '19 at 14:53

1 Answers1

1

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);
Cerad
  • 48,157
  • 8
  • 90
  • 92
  • well your solution works but I need something dynamic to fix all routes. – Teoman Tıngır Nov 07 '19 at 16:19
  • Any reason you are not using the available [route loading](https://symfony.com/doc/current/components/routing.html#loading-routes) functionality? Basically just need to add the symfony/config component. Seems like it would save you a great deal of trouble. Or if you want to stick with your own solution then search for $trailingSlashOnRoot in the routing component to see how to implement a more general approach. – Cerad Nov 07 '19 at 16:27
  • I already in use with another config loader. I don't want to change whole system for that. But I'm very appreciate your effort, I couldn't figure out if you don't help me – Teoman Tıngır Nov 07 '19 at 17:17