是否可以在Symfony中更改默认重定向消息? [英] Is it possible to change the default redirect message in Symfony?

查看:124
本文介绍了是否可以在Symfony中更改默认重定向消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用户更改网站语言后,我正在使用控制器来重定向他们.

I'm using a controller to redirect my users after they have changed the website's language.

return $this->redirect($this->generateUrl($_redirectTo), 301);

问题是,出现了消息重定向到/path/",这是我不想看到的. 可以更改该消息吗?

The problem is, that the message "redirecting to /path/" shows up, which i don't want to. Is it possible to change that message?

推荐答案

方法 Controller::redirect() 实际上是在创建一个新的硬编码进入响应,但是这里有一些解决方法.

The method Controller::redirect() is in fact, creating a new RedirectResponse object.
The default template is hard-coded into the response but here are some workarounds.

在此示例中,我将使用TWIG模板,因此我需要@templating服务,但是您可以使用任何要呈现页面的内容.

In this example, I will use a TWIG template, hence I need the @templating service, but you can use whatever you want to render the page.

首先,使用所需的内容在Acme/FooBundle/Resources/views/Error/中创建模板301.html.twig.

First, create your template 301.html.twig into your Acme/FooBundle/Resources/views/Error/ with the content you 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>
        You are about to be redirected to {{ uri }}
    </body>
</html>

来自事件监听器

如果希望此模板在任何RedirectResponse上都是全局的,则可以创建一个事件侦听器,该侦听器将侦听响应并检查给定的响应是否为RedirectResponse
的实例. 这意味着您仍然可以在控制器中使用return $this->redirect,只有响应的内容会受到影响.

From an event listener

If you want this template to be global on any RedirectResponse you can create an event listener which will listen to the response and check whether the response given is an instance of RedirectResponse
This means that you can still use return $this->redirect in your controller, only the content of the response will be 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 a controller

Use this if you want to change the template directly from an action.
The modification will only be available for the given action, not global to your 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;
    }
}

这篇关于是否可以在Symfony中更改默认重定向消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆