gpt4 book ai didi

php - Symfony - 公共(public)和管理部分的不同错误页面

转载 作者:行者123 更新时间:2023-12-03 23:10:23 24 4
gpt4 key购买 nike

我一直在关注 http://symfony.com/doc/current/cookbook/controller/error_pages.html 上的提示并在 Resources/TwigBundle/views/Exceptions 中创建了新模板 error500.html.twig。

这工作正常,但如果用户在网站的网络或管理部分,我希望有不同的页面。

有没有简单的方法来做到这一点?谢谢你,迈克。

最佳答案

我认为最好的方法是 overriding the default ExceptionController .只需扩展它,并覆盖 findTemplate 方法。从请求的属性中检查是否设置了 _route_controller,并对其进行处理。

namespace AppBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\TwigBundle\Controller\ExceptionController as BaseExceptionController;

class ExceptionController extends BaseExceptionController
{
protected function findTemplate(Request $request, $format, $code, $showException)
{
$routeName = $request->attributes->get('_route');

// You can inject these routes in the construct of the controller
// so that you can manage them from the configuration file instead of hardcode them here
$routesAdminSection = ['admin', 'admin_ban', 'admin_list'];

// This is a poor implementation with in_array.
// You can implement more advanced options using regex
// so that if you pass "^admin" you can match all the routes that starts with admin.

// If the route name match, then we want use a different template: admin_error_CODE.FORMAT.twig
// example: admin_error_404.html.twig
if (!$showException && in_array($routeName, $routesAdminSection, true)) {
$template = sprintf('@AppBundle/Exception/admin_error_%s.%s.twig', $code, format);
if ($this->templateExists($template)) {
return $template;
}

// What you want to do if the template doesn't exist?
// Just use a generic HTML template: admin_error.html.twig
$request->setRequestFormat('html');
return sprintf('@AppBundle/Exception/admin_error.html.twig');
}

// Use the default findTemplate method
return parent::findTemplate($request, $format, $code, $showException);
}
}

然后配置twig.exception_controller:

# app/config/services.yml
services:
app.exception_controller:
class: AppBundle\Controller\ExceptionController
arguments: ['@twig', '%kernel.debug%']

# app/config/config.yml
twig:
exception_controller: app.exception_controller:showAction

然后您可以用相同的方式覆盖模板:

  • 资源/AppBundle/views/Exceptions/
    • admin_error.html.twig
    • admin_error_404.html.twig
    • admin_error_500.html.twig
    • ...

更新

一个更简单的方法是在您的路由的 defaults 集合中指定网站的部分。示例:

# app/config/routing.yml
home:
path: /
defaults:
_controller: AppBundle:Main:index
section: web
blog:
path: /blog/{page}
defaults:
_controller: AppBundle:Main:blog
section: web
dashboard:
path: /admin
defaults:
_controller: AppBundle:Admin:dashboard
section: admin
stats:
path: /admin/stats
defaults:
_controller: AppBundle:Admin:stats
section: admin

然后你的 Controller 变成这样:

namespace AppBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\TwigBundle\Controller\ExceptionController as BaseExceptionController;

class ExceptionController extends BaseExceptionController
{
protected function findTemplate(Request $request, $format, $code, $showException)
{
$section = $request->attributes->get('section');
$template = sprintf('@AppBundle/Exception/%s_error_%s.%s.twig', $section, $code, format);
if ($this->templateExists($template)) {
return $template;
}

return parent::findTemplate($request, $format, $code, $showException);
}
}

并以与上述相同的方式配置twig.exception_controller。现在您只需为每个部分、代码和格式定义一个模板。

  • web_error_404.html.twig
  • web_error_500.html.twig
  • admin_error_404.html.twig
  • admin_error_500.html.twig
  • 等等...

关于php - Symfony - 公共(public)和管理部分的不同错误页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36524254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com