首页 > 解决方案 > 如何在奏鸣曲管理员中为路由设置主机?

问题描述

如何让奏鸣曲始终使用根域作为它生成的 url?我有一个站点,有些页面在根域上,有些在子域上。我需要有 url 来编辑始终引用根域的帖子,即使链接在子域上也是如此。

<a href="{{ path('admin_prefix_post_post_edit', {id: post.id}) }}" rel="nofollow" target="_blank">Edit</a>

标签: symfonysonata-adminsonatasymfony-sonata

解决方案


看起来我可以解决奏鸣曲添加到奏鸣曲路由选项和 routing.yml 中的默认值的问题

admin_area:
    resource: "@SonataAdminBundle/Resources/config/routing/sonata_admin.xml"
    prefix: /backoffice
    options:
        compiler_class: 'MyApp\Router\RouteCompiler'
    defaults:
        default_host: %router.request_context.host%

_sonata_admin:
    resource: .
    type: sonata_admin
    prefix: /backoffice
    options:
        compiler_class: 'MyApp\Router\RouteCompiler'
    defaults:
        default_host: %router.request_context.host%

router.request_context.host 是我的根域。

路由编译器类如下所示:

class RouteCompiler extends BaseRouteCompiler
{
    public static function compile(Route $route)
    {
        if ($route->getHost() === "") {
            $route->setHost($route->getDefaults()['default_host']);
        }
        return parent::compile($route);
    }
}

当我在 twig url 函数中使用时,即使在来自子域的页面上,它也会生成根域的 url。


推荐阅读