Symfony 自定义错误 404 和 500

Symfony customizing error 404 and 500

我读了 Symfony (http://symfony.com/doc/current/cookbook/controller/error_pages.html#testing-error-pagesdocumentation) 并按照他们说的做了:

我在目录 (myproject)/app/Resources/TwigBundle/views/Exception/

中创建了 2 个文件,error404.html.twig 和 error500.html.twig

然后我像这样更改了 routing_dev.yml 文件:

_wdt:
    resource: "@WebProfilerBundle/Resources/config/routing/wdt.xml"
    prefix:   /_wdt

_profiler:
    resource: "@WebProfilerBundle/Resources/config/routing/profiler.xml"
    prefix:   /_profiler

_configurator:
    resource: "@SensioDistributionBundle/Resources/config/routing/webconfigurator.xml"
    prefix:   /_configurator

_main:
    resource: routing.yml

_errors:
    resource: "@TwigBundle/Resources/config/routing/errors.xml"
    prefix:   /_error

当我试着用 http://localhost:8080/portfolio/web/app_dev.php/_error/404

看看它是什么样子时

我收到以下错误:

FileLoaderLoadException:无法从 "C:\xampp\htdocs\portfolio\app/config/routing_dev.yml" 导入资源“@TwigBundle/Resources/config/routing/errors。xml”。确保 "TwigBundle/Resources/config/routing/errors.xml" 包在应用程序内核 class.

中正确注册和加载

我检查了我的 AppKernel.php 文件,似乎导入了 twigBundle:

<?php

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),
            new Symfony\Bundle\TwigBundle\TwigBundle(),
            new Symfony\Bundle\MonologBundle\MonologBundle(),
            new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
            new Symfony\Bundle\AsseticBundle\AsseticBundle(),
            new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
            new AppBundle\AppBundle(),
            new PublicBundle\PublicBundle(),
            new AdminBundle\AdminBundle(),
        );

        if (in_array($this->getEnvironment(), array('dev', 'test'))) {
            $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
            $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
            $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
        }

        return $bundles;
    }

    public function registerContainerConfiguration(LoaderInterface $loader)
    {
        $loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');
    }
}

有什么问题?

除非必要,否则无需为此创建自定义捆绑包即可执行此操作。

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

在 app/Resources/TwigBundle/views/Exception/error.html.twig 下创建一个 error.html.twig 等其他特殊错误,如:app/Resources/TwigBundle/views/Exception/error404.html.twig

我发现了问题。这真的很有趣。

我使用的是 Symfony 2.5.2,但我尝试使用的功能是 Symfony 2.6

所以我在 2.6 中创建了一个新项目,替换并重新配置我的文件夹和文件,然后 BOOM!太棒了!