我阅读了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/