Skip to content

Insufficient explanation for service configuration in "Routing" #1713

@falkgeist

Description

@falkgeist

I am not sure if I am misunderstanding, but it seems that the example is not helping quite enough to show how to implement the service as public or tag it with the controller.service_arguments tag...

Admittedly, the AsController attribute does that, but from the text and the code, this is not exactly clear for anyone.

It would help in my opinion if this explanation from the Symfony docs is either referenced or quoted, adding the information about when and how to use which method: AbstractController, AsController or manual controller.service_arguments tag. Although I do think the latter should probably be omitted from the Contao docs and only referenced...

(What added to my confusion, but is an honest mistake, was the leftover use for the ServiceTag annotation and the Routing annotation. 😉)

When using controllers as services and taking advantage of dependency injection,
the controller's service needs to be set to `public` or be tagged with the
`controller.service_arguments` tag.
```php
// src/Controller/ExampleController.php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Terminal42\ServiceAnnotationBundle\Annotation\ServiceTag;
#[Route('/example', name: ExampleController::class)]
#[AsController]
class ExampleController
{
public function __construct(
private readonly AuthorizationCheckerInterface $authorizationChecker
) {
}
public function __invoke(): Response
{
if ($this->authorizationChecker->isGranted('ROLE_MEMBER')) {
return new Response('Member is logged in.');
}
return new Response ('Member is not logged in.');
}
}
```

I am happy to provide a PR, but I wanted to check first if I misunderstood or not.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions