2

I would like to reduce the number of repetitive code and give a canonical URL in my Drupal 8 application. Since the routing system is built on Symfony, I included it in the title.

I am constructing paths under routes in my mymodule.routing.yml file. I want to match a specified number of different strings in the first argument, and a slug which can be any string in the second argument. It looks like this:

entity.my_entity.canonical:
  path: '/{type}/{slug}'
  defaults:
    _controller: '\namespace\PostController::show'
  requirements:
    _permission: 'perm'
    type: different|strings|that|can|match|

Now, when I try to access using for example /match/some-slug then it just says "Page not found". If I something static to the path, for example path: '/j/{type}/{slug}', then it works as expected when I open /j/match/some-slug in the browser.

My boss doesn't like any unnecessary characters in the URL though, so I would like to achieve this by using two parameters, like shown in the first example.

3
  • Make sure there isn't another path that could have conflict with it, because it's possible perfectly. Commented Nov 23, 2016 at 18:02
  • How could I find out? Would it be considered a conflict if another path has two parameters as well, or only if the parameters have the same name? Commented Nov 23, 2016 at 18:33
  • 2
    Use php bin/console debug:router to debug your defined routes, and checks the order also Commented Nov 23, 2016 at 18:41

2 Answers 2

1

As Yonel mentioned in the comments you can use debug:router to check all your routes. I don't see anything wrong with your code.

Try running bin/console router:match "/match/blaaa" and if you see some controller that isn't the one you want then you'll need to change the route. It shouldn't be the case though because you're getting a 404.

Here's my exact setup that works

routing.yml:

entity.my_entity.canonical:
  path: '/{type}/{slug}'
  defaults:
    _controller: 'MyBundle:Something:foo'
  requirements:
    type: different|strings|that|can|match|

Inside MyBundle\SomethingController:

public function fooAction($id)
{
  return new Response("bar");
}

Then going to http://localhost/match/fom shows the "bar" response.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your help, I found out now that it is not possible in Drupal 8. And for other Drupal people: the command to get a list of routes (with Drupal Console) is drupal router:debug
0

I have read the documentation again (RTM), and found out that it is not possible in Drupal 8, while it is possible in Symfony.

Note that the first item of the path must not be dynamic.

Source: Structure of routes in Drupal 8

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.