Skip to content

Injecting SiteAccess

The service container exposes the SiteAccess through the @ezpublish.siteaccess_service service, which fulfills the \eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessServiceInterface contract. This means you can inject it into any custom service constructor, type hinting that contract. You can get the current SiteAccess from that service by calling the SiteAccessServiceInterface::getCurrent method.

For example, define a service which depends on the Repository's ContentService and the SiteAccessService.

1
2
3
services:
    App\MyService:
        arguments: ['@ezpublish.siteaccess_service']
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
declare(strict_types=1);

namespace App;

use eZ\Publish\API\Repository\ContentService;
use eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessServiceInterface;

class MyService
{
    /** @var \eZ\Publish\API\Repository\ContentService */
    private $contentService;

    /** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessServiceInterface */
    private $siteAccessService;

    public function __construct(
        SiteAccessServiceInterface $siteAccessService,
        ContentService $contentService
    ) {
        $this->siteAccessService = $siteAccessService;
        $this->contentService = $contentService;
    }
}