php - Is it possible to change the default redirect message in Symfony? -
i'm using controller redirect users after have changed website's language.
return $this->redirect($this->generateurl($_redirectto), 301);
the problem is, message "redirecting /path/" shows up, don't want to. possible change message?
the method controller::redirect()
in fact, creating new redirectresponse
object.
default template hard-coded response here workarounds.
in example, use twig template, hence need
@templating
service, can use whatever want render page.
first, create template 301.html.twig
acme/foobundle/resources/views/error/
content want.
@acmefoobundle/resources/views/error/301.html.twig
<!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <meta http-equiv="refresh" content="1;url={{ uri }}" /> </head> <body> redirected {{ uri }} </body> </html>
from event listener
if want template global on redirectresponse
can create event listener listen response , check whether response given instance of redirectresponse
means can still use return $this->redirect
in controller, content of response affected.
services.yml
services: acme.redirect_listener: class: acme\foobundle\listener\redirectlistener arguments: [ @templating ] tags: - name: kernel.event_listener event: kernel.response method: onkernelresponse
acme\foobundle\listener\redirectlistener
use symfony\component\templating\engineinterface; use symfony\component\httpkernel\event\filterresponseevent; use symfony\component\httpfoundation\redirectresponse; class redirectlistener { protected $templating; public function __construct(engineinterface $templating) { $this->templating = $templating; } public function onkernelresponse(filterresponseevent $event) { $response = $event->getresponse(); if (!($response instanceof redirectresponse)) { return; } $uri = $response->gettargeturl(); $html = $this->templating->render( 'acmefoobundle:error:301.html.twig', array('uri' => $uri) ); $response->setcontent($html); } }
from controller
use if want change template directly action.
modification available given action, not global application.
use symfony\component\httpfoundation\redirectresponse; use symfony\component\httpkernel\event\filterresponseevent; class foocontroller extends controller { public function fooaction() { $uri = $this->generateurl($_redirectto); $response = new redirectresponse($uri, 301); $response->setcontent($this->render( 'acmefoobundle:error:301.html.twig', array( 'uri' => $uri ) )); return $response; } }
Comments
Post a Comment