简体   繁体   中英

How to set host for routes in sonata admin?

How to make sonata always use root domain for url it generates? I have a site, some of the pages are on the root domain, some are on the subdomain. I need to have url to edit post that always refers to root domain even if the link is on the subdomain.

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

It looks like I could solve the issue for sonata adding to sonata route options and defaults in 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 is my root domain.

The route compiler class looks like:

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

When I use in twig url function, it generates url to root domain even on pages from subdomain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM