Karolis
Karolis

Reputation: 2618

Get symfony route from controller action annotations

I have a controller action like this:

/**
 * @Route("/post/delete/{id}", name="delete_post_modal")
 */
public function deleteAction(Post $post)
{
    // ...
}

The annotation @Route tells symfony to execute method deleteAction when route matches delete_post_modal. All of this works fine.

Question: Is there a way to reverse this functionality and get the route name from method name?

From the example above:

thanks!

Upvotes: 1

Views: 1768

Answers (1)

ghaziksibi
ghaziksibi

Reputation: 471

try this

update

 $router = $this->container->get('router');

foreach ($router->getRouteCollection()->all() as $route => $params)
{
    $defaults = $params->getDefaults();

    if ( strpos($defaults['_controller'],'PostController::deleteAction') ) {
        $myroute = $route;
        break;
    }
}

Upvotes: 4

Related Questions