Symfony笔记之--自定义异常提示
Custom Exception
- ExceptionController
namespace KitBaseBundle\Controller; use Symfony\Bundle\TwigBundle\Controller\ExceptionController as BaseController; use Symfony\Component\HttpFoundation\Request; use KitAdminBundle\Service\ThemeService; class ExceptionController extends BaseController { private $theme; public function __construct(\Twig_Environment $twig, $debug,ThemeService $theme) { parent::__construct($twig, $debug); $this->theme = $theme; } /** * @param Request $request * @param string $format * @param int $code An HTTP response status code * @param bool $showException * * @return string */ protected function findTemplate(Request $request, $format, $code, $showException) { $themeName = $this->theme->get(); $name = $showException ? 'exception' : 'error'; if ($showException && 'html' == $format) { $name = 'exception_full'; } // For themes error pages if (!$showException) { $template = sprintf('@KitWeb/theme/'.$themeName.'/Exception/%s%s.%s.twig', $name, $code, $format); if ($this->templateExists($template)) { return $template; } } // try to find a template for the given format $template = sprintf('@KitWeb/theme/'.$themeName.'/Exception/%s.%s.twig', $name, $format); if ($this->templateExists($template)) { return $template; } return parent::findTemplate($request, $format, $code, $showException); } }
- ThemeService.php
<?php namespace KitAdminBundle\Service; class ThemeService { /** * * @var \Doctrine\Bundle\DoctrineBundle\Registry */ private $doctrine; public function __construct($doctrine) { $this->doctrine = $doctrine; } public function get($default = 'Default') { /** * * @var \KitAdminBundle\Repository\ThemeRepository $repo */ $repo = $this->doctrine->getRepository('KitAdminBundle:Theme'); $theme = $repo->findOneBy([ 'status' => 1 ]); return (!empty($theme) && !empty($theme->getPath())) ? $theme->getPath() : $default; } }
config.yml
# Twig Configuration twig: .... exception_controller: twig.controller.exception:showAction
service.yml
kit_admin.theme_service: class: KitAdminBundle\Service\ThemeService arguments: ["@doctrine"] twig.controller.exception: class: KitBaseBundle\Controller\ExceptionController arguments: ['@twig','%kernel.debug%', '@kit_admin.theme_service']
views
# KitWebBundle/Resources/views/theme/Luxe/Exception/error.html.twig <h2>{{ theme_name }} self excption error</h2>
相关推荐
xcguoyu 2020-02-26
samtrue 2019-12-26
宋大人 2014-06-10
清风徐来水波不兴 2019-12-25
CoderLiu 2019-11-19
luadenis0 2014-06-05
klarclm 2013-07-05
wangyan 2019-09-08
tianyin 2019-07-21
wangdianyong 2019-07-21
duanshui 2019-06-30
ahxxx 2019-06-30
宋大人 2014-06-10
爱自由 2019-06-29
爱自由 2019-06-29
shana0 2019-06-28
liuwendao 2016-10-19
爱自由 2019-06-28