i have a navigation menu available on all my pages. when a user click on one link , it open a modal form where user can send a message. when a user click on submit button, i want the controller return on the page where he was when the modal opened.
-
what have you created the route for page ? or module ? in `routing.yml` ? – Noman Feb 11 '16 at 09:24
-
1Possible duplicate of [How to get current route in Symfony 2?](http://stackoverflow.com/questions/7096546/how-to-get-current-route-in-symfony-2) – zizoujab Feb 11 '16 at 09:54
-
i have edited my question to be clearer : it ' not the current page but the page where the user was when he clicked to open the form in the lightbox – sepointes Feb 11 '16 at 10:17
3 Answers
To get current route in controller
.
$router = $this->get("router");
$route = $router->match($this->getRequest()->getPathInfo());
var_dump($route['_route']);
OR
$request = $this->container->get('request');
$routeName = $request->get('_route');
To get current route in twig
app.request.get('_route')

- 4,088
- 1
- 21
- 36
-
thanks but it doesn'work, with this code i get the route of the controller where i am – sepointes Feb 11 '16 at 09:34
-
you asked to get current route. my answer return the correct current route in twig . you need to add a hidden field and put the current route in that. then change the modal submit action acordingly – Noman Feb 11 '16 at 09:45
-
yes i think hidden field is the only solution, i will try this. thanks – sepointes Feb 11 '16 at 09:48
Okay, so post is a little old but for anyone else looking for something like this I would suggest a different way of handling the message form using javascript to intercept the submission and send the serialised data via an ajax post.
The controller can then return a simple success message or the form view with any error message.
The user could try resending the message or just close the modal, or you close it automatically for them with a window.timeout if the submission was successful. The user never leaves the original page and they dont have to wait for a page reload.
A library like jQuery will make this fairly easy to implement eg
$(document).on("click","button#messageSend",function(e){
tht = $(this);
frm = tht.closest("form");
container = frm.parent()
e.preventDefault();
$.ajax({
url: frm.attr("action"),
method: "POST",
data: frm.serialize(),
dataType: "html"
}).done(function(data){
container.html(data);
// Add any additional processing here
});
return false;
});

- 33
- 5
To generate path in twig template:
{{ path(app.request.attributes.get('_route'),
app.request.attributes.get('_route_params')) }}
or to get current request uri:
{{ app.request.uri }}
You might also consider doing the modal window in an overlay, so a user could return to current page just by closing the modal.

- 2,580
- 2
- 16
- 31
-
`{{ app.request.uri }}` give me the route for open message form so it's not what i want. yes a user can return to current page by closing but when he send a message i have to specify a route to my controller and i want return on current page just as if user closed the modal – sepointes Feb 11 '16 at 09:47
-
If you are trying to use `{{ app.request.uri }}` in a request which actually handles the message sending, it will not work as it is a separate http request... You might want to pass the return path as a hidden form input, but honestly, with the amount of details you provided, I don't think you will get much help. – Karolis Feb 11 '16 at 10:11