如何在symfony中将404重定向到home?

时间:2016-01-21 12:10:45

标签: php symfony

我想重定向此网址http://www.businessbid.ae/stagging/web/feedbackhttp://www.businessbid.ae。我该怎么办?

3 个答案:

答案 0 :(得分:6)

你应该创建一个监听器来监听onKernelExceptionEvent 您可以检查404状态代码并设置重定向响应。

的appbundle \事件监听\ Redirect404ToHomepageListener

namespace AppBundle\EventListener;

use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\Routing\RouterInterface;

class Redirect404ToHomepageListener
{
    /**
     * @var RouterInterface
     */
    private $router;

    /**
     * @var RouterInterface $router
     */
    public function __construct(RouterInterface $router)
    {
        $this->router = $router;
    }

    /**
     * @var GetResponseForExceptionEvent $event
     * @return null
     */
    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        // If not a HttpNotFoundException ignore
        if (!$event->getException() instanceof NotFoundHttpException) {
            return;
        }

        // Create redirect response with url for the home page
        $response = new RedirectResponse($this->router->generate('home_page'));

        // Set the response to be processed
        $event->setResponse($response);
    }
}

services.yml

services:
    app.listener.redirect_404_to_homepage:
        class: AppBundle\EventListener\Redirect404ToHomepageListener
        arguments:
            - "@router"
        tags:
            - { name: kernel.event_listener, event: kernel.exception, method: onKernelException }

答案 1 :(得分:1)

在你的控制器中试试这个:

$this->redirect($this->generateUrl('route_name')));

答案 2 :(得分:1)

您需要覆盖默认Exception Controller。恕我直言更好的解决方案是在@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_lcd_screen_setting); initUI(); setBrightness(); } private void setBrightness() { Brighness.setMax(255); float curBrightnessValue = 0; try { curBrightnessValue = android.provider.Settings.System.getInt( getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } int screen_brightness = (int) curBrightnessValue; Brighness.setProgress(screen_brightness); Brighness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { int progress = 0; @Override public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) { progress = progresValue; } @Override public void onStartTrackingTouch(SeekBar seekBar) { // Do something here, // if you want to do anything at the start of // touching the seekbar } @Override public void onStopTrackingTouch(SeekBar seekBar) { android.provider.Settings.System.putInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, progress); } }); } initUI(){ Brighness = (SeekBar) findViewById(R.id.brightnessbar); } 或nginx配置中完成这项工作。