Skip to content

Commit

Permalink
add service reference
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzmg committed Sep 29, 2019
1 parent 51aa123 commit 89850dd
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 11 deletions.
9 changes: 0 additions & 9 deletions docs/dev/framework/services.md

This file was deleted.

141 changes: 139 additions & 2 deletions docs/dev/reference/services.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,144 @@
---
title: Services
description: A list of useful services that can be used for custom development.
description: Overview of Contao's Symfony Services relevant to your development.
aliases:
- /framework/services/
---


TODO
{{% notice info %}}
This list is still incomplete.
{{% /notice %}}


## ContaoFramework

This service manages and initializes Contao's legacy framework. If you need any
functionality from the legacy framework (e.g. Models) or you want to create adapters
for Contao's static classes, then you need to use this service.

```php
use Contao\CoreBundle\Framework\ContaoFramework;

class Example
{
private $framework;

public function __consruct(ContaoFramework $framework)
{
$this->framework = $framework;
}

public function execute()
{
$this->framework->initialize();

$contentElement = \Contao\ContentModel::findByPk(…);

$system = $this->framework->getAdapter(\Contao\System::class);
$system->loadLanguageFile('default');

// …
}
}
```


## ScopeMatcher

This service provides the ability to identify the Contao scope of a request, if
applicable. It should be used instead of checking the deprecated `TL_MODE` constant.

```php
use Contao\CoreBundle\Routing\ScopeMatcher;
use Symfony\Component\HttpFoundation\RequestStack;

class Example
{
private $requestStack;
private $scopeMatcher;

public function __construct(RequestStack $requestStack, ScopeMatcher $scopeMatcher)
{
$this->requestStack = $requestStack;
$this->scopeMatcher = $scopeMatcher;
}

public function isBackend()
{
return $this->scopeMatcher->isBackendRequest($this->requestStack->getCurrentRequest());
}

public function isFrontend()
{
return $this->scopeMatcher->isFrontendRequest($this->requestStack->getCurrentRequest());
}
}
```


## Symfony\Component\Security\Core\Security

Not directly related to Contao, but this helper service from Symfony lets you retrieve
the current Contao front end or back end user from the firewall.

```php
use Contao\BackendUser;
use Contao\FrontendUser;
use Symfony\Component\Security\Core\Security;

class Example
{
private $security;

public function __construct(Security $security)
{
$this->security = $security;
}

public function execute()
{
if (($backendUser = $this->security->getUser()) instanceof BackendUser) {
// …
}

if (($frontendUser = $this->security->getUser()) instanceof FrontendUser) {
// …
}
}
}
```


## TokenChecker

This service let's you query information of the Contao related security tokens, if
present. It allows you to check, whether a token for a front end user, back end
user or the preview mode is present. It also allows you to retrieve the username
of the token.


```php
use Contao\CoreBundle\Security\Authentication\Token\TokenChecker;
use Contao\BackendUser;
use Contao\FrontendUser;

class Example
{
private $tokenChecker;

public function __construct(TokenChecker $tokenChecker)
{
$this->tokenChecker = $tokenChecker;
}

public function execute()
{
if ($this->tokenChecker->hasFrontendUser()) { /* … */ }
if ($this->tokenChecker->hasBackendUser()) { /* … */ }
if ($this->tokenChecker->isPreviewMode()) { /* … */ }
if (null !== ($frontendUsername = $this->tokenChecker->getFrontendUsername())) { /* … */ }
if (null !== ($backendUsername = $this->tokenChecker->getBackendUsername())) { /* … */ }
}
}
```

0 comments on commit 89850dd

Please sign in to comment.