1

I hope I may ask this question without going into the details of why I need to do this,

I have a route

 /**
 * @Route("/edit/{id}", name="my_edit_route")
 */
public function editAction()
{  /* ....... */ }

And now at some other point in time, I have a URL which was generated with this route (I only have the string, no other information is available). The string looks like this

/edit/23

Is there anyway in Symfony2, that I can provide the route name (e.g. "my_edit_route" and this URL string, and have Symfony extract the parameters. (e.g. the id=23)

p.s. I know this could be done with regex or other tools, but my actual routes in my application are more complex.

I am sure this should not be to difficult for Symfony2 to do, as it already does this each time it extracts the parameters from your URL to handle a request.

Sam Anthony
  • 1,669
  • 2
  • 22
  • 39

1 Answers1

5

you no need to route name

$params = $this->get('router')->match('/edit/23');

that return $params as:

array(
    'id'          => '23',
    '_controller' => 'AppBundle:Blog:show',
)
ghanbari
  • 1,630
  • 1
  • 16
  • 31
  • How do you recommend to retrive the `/edit/23` part from a full URL like `http://server/myproject/web/app.dev/edit/23`? – Samiron Sep 27 '15 at 11:17
  • @Samiron use getPathInfo() method of Request class. `$request->getPathInfo()`, note: you may need use urldecode function because this method return encoded value, see api: http://api.symfony.com/2.7/Symfony/Component/HttpFoundation/Request.html – ghanbari Sep 28 '15 at 06:49
  • Thanks for reply. Perhaps you can take a look at http://stackoverflow.com/q/32808325/1160106. I tried to explain my problem in detail there. `getPathInfo()` is not really working for my case. – Samiron Sep 28 '15 at 15:28