Symfony自定义错误404和500

时间:2015-03-16 05:32:50

标签: symfony templates error-handling

我读了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”包。

我检查了我的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');
    }
}

问题是什么?

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

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

所以我在2.6中创建了一个新项目,替换和重新配置我的文件夹和文件以及BOOM!它wotks!