gpt4 book ai didi

symfony - 通过覆盖ExceptionController的Symfony自定义错误页面

转载 作者:行者123 更新时间:2023-12-03 07:42:07 24 4
gpt4 key购买 nike

我想要做的是拥有一个自定义错误页面,不仅它们会扩展基本布局,而且我也希望额外出售这些页面中的内容,因此仅更改模板是不可行的

无论原因为何(404未找到或只是缺少变量),我想显示我的模板和内容

我已经花了几个小时试图使这一切顺利

app/console --version
Symfony version 2.5.6 - app/dev/debug

我尝试了一些资源,但无法正常工作。几个名字:

http://symfony.com/doc/current/reference/configuration/twig.html
http://symfony.com/doc/current/cookbook/controller/error_pages.html

我在没有调试的dev中运行,请参见下面的 app_dev.php:
$kernel = new AppKernel('dev', false);

在教程之后,我得到了这些额外的信息
app/config/config.yml
twig:
exception_controller: SomethingAppBundle:Exception:show

在我的 bundle 包中
<?php

namespace Something\AppBundle\Controller;

use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
use Symfony\Component\HttpKernel\Exception\FlattenException;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class ExceptionController extends Controller
{
public function showAction( FlattenException $error, DebugLoggerInterface $debug)
{
print_r($error);
}

}

但我的错误 Controller 无法执行,

我故意通过尝试在其他 Controller 中回显 undefined variable 而导致错误,因为它应该处理整个应用程序中的错误

最佳答案

首先,您需要在 Controller 中创建操作:

<?php
namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class ErrorController extends Controller
{

public function notFoundAction()
{
return $this->render('error/404.html.twig');
}
}
然后,您需要创建一个侦听器:
<?php
namespace AppBundle\EventListener;

use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\HttpKernelInterface;

class NotFoundHttpExceptionListener
{
private $controller_resolver;
private $request_stack;
private $http_kernel;

public function __construct($controller_resolver, $request_stack, $http_kernel)
{
$this->controller_resolver = $controller_resolver;
$this->request_stack = $request_stack;
$this->http_kernel = $http_kernel;
}
public function onKernelException(GetResponseForExceptionEvent $event)
{

if ($event->getException() instanceof NotFoundHttpException) {

$request = new \Symfony\Component\HttpFoundation\Request();
$request->attributes->set('_controller', 'AppBundle:Error:notFound');
$controller = $this->controller_resolver->getController($request);

$path['_controller'] = $controller;
$subRequest = $this->request_stack->getCurrentRequest()->duplicate(array(), null, $path);

$event->setResponse($this->http_kernel->handle($subRequest, HttpKernelInterface::MASTER_REQUEST)); // Simulating "forward" in order to preserve the "Not Found URL"

}
}
}
现在注册服务:
#AppBundle/Resources/config/services.yml
services:
kernel.listener.notFoundHttpException:
class: AppBundle\EventListener\NotFoundHttpExceptionListener
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException, priority: -10 }
arguments: [ @controller_resolver, @request_stack, @http_kernel ]
尚未对此进行测试,但是应该可以工作;)
编辑:
经过测试,可以正常工作。在呈现的页面上,您拥有一个 session ,因此您可以访问app.user,他的购物车以及与该 session 相关的其他事项。

关于symfony - 通过覆盖ExceptionController的Symfony自定义错误页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27109470/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com