phpsymfonyroutes

router match url with parameters


In my Symfony2 project i have a route with parameters:

my_route:
    pattern:  /{param1}/{param2}
    defaults: { _controller: MyBundle:MyController:myAction }

and in the action myAction i get the url and i when i try to get the corresponding route by matching the url i get this error:

 500 Internal Server Error - ResourceNotFoundException 

and then the stack trace show this message :

1. in C:\Users\itaziny\git\Symfony\app\cache\dev\appDevUrlMatcher.php at line 459 

  throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new ResourceNotFoundException();

this my code:

public function myActionAction(Request $request) {
   $url = $request->headers->get('referer');
   $router = $this->get('router');
   $route = $router->match($url);
   // Some code...

   if (route == "my_route") {
       // redirect to the pag: my_route
   }
   else {
      //redirect to the page who called this action
   }
}

the action: myAction is called from 2 different pages, and i have to redirect to the page who called the action myAction


Solution

  • i finally find the answer here : Symfony2: Redirecting to last route and flash a message?

    so what i did exactly is this:

    $uri = $request->headers->get('referer');
    $baseUrl = $request->getBaseUrl();
    $lastPath = substr($uri, strpos($uri, $baseUrl) + strlen($baseUrl));
    
    $route = $this->get('router')->match($lastPath);
    if ($route['_route'] == "my_route") {
        // redirect to the pag: my_route
    }
    else {
      //redirect to the page who called this action
    

    }