symfony - Symfony自定义错误404和500

标签 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” bundle 包已正确注册并加载到应用程序内核类中。

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

有什么问题吗?

最佳答案

除非必要,否则无需创建自定义 bundle 包就可以做到这一点。

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 - Symfony自定义错误404和500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29070178/

相关文章:

javascript - 在 Twig 模板上从数据库中取消转义 HTML 字符串

php - 通过 AJAX 模板化动态加载内容的最佳方法?

html - Liferay 7 Freemarker 模板。 staticUtil 已评估为 NULL 或缺失 - 尝试获取 JournalArticle 的类别

php - 在PHP魔术方法上处理 “not found”案例的最佳方法是什么?

jenkins - 我何时以及为什么应该在 Jenkinsfiles catch block 中抛出错误? (脚本化管道)

php - 具有多个连接的查询生成器出现异常

php - apache2 多个虚拟主机,但只有第一个有效

php - 覆盖子包中的 Doctrine Mappgins

c++ - Switch 语句可变模板扩展

windows - "was unexpected at this time."