route: getNamedRoute()->getPath() returning the route, with wildcard?

route 3.1

I’m curious if I’m doing something wrong, if I have a misconfiguration somewhere or if this is expected. Calling getNamedRoute('the-route-name')->getPath() from the RouteCollection returns the route name, with wildcards, is this expected behaviour?

Example route: $route->get('/the-route/{id:number}/', Action::class.'::index')->setName('the-route-name');

Currently I feel like I’m hacking around it by doing:

        if (strpos($this->route->getNamedRoute($name)->getPath(), '{') !== false) {
            return substr($this->route->getNamedRoute($name)->getPath(), 0,
                strrpos($this->route->getNamedRoute($name)->getPath(), "{"));
        } else {
            return $this->route->getNamedRoute($name)->getPath();
        }

But that’s because I’m just unsure if I should be getting the route path with the wildcards or not…

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 24 (12 by maintainers)

Most upvoted comments

See release 4.5.0

Yep, I think the example you provided of getPath(['wildcard' => 'example', 'id' => 1]) would be nice.

Okay so that is what I’m suggesting but you’re just stripping the wildcard part and adding a value to the end, what I’m suggesting is to allow you to pass an array to getPath that would replace the wildcards before it returns the path to you, otherwise you’ll have issues when it comes wildcards in different places.

I’m assuming you could pass an array to routeFor in this example via twig? It’s been a while since I’ve used it.

Well specifically this isn’t a framework, so you want to use a named route to generate a link with the wildcards replaced?

I’d accept a PR where the getPath method accepts an array of replacements for the wildcards.

Sent from ProtonMail Mobile

On Sun, Feb 23, 2020 at 06:57, alturic notifications@github.com wrote:

Well, I’m trying to use the route names in templates. What’s weird is that of all the frameworks and twig integrations I’ve seen, I swear they all look like to get a named url they call the exact same method and don’t have that issue.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.