Hallo,
ich bin Chris und neu dabei. Ich entwickle mit html Code und Controllern eine Seite in Contao.
Ich versuche eine Security Abfrage im Controller durchzuführen. Ich habe mir erstmal angeschaut, wie der Standard genutzt wird mit dem Beispiel:
Code:
// src/Controller/BackendController.php
namespace App\Controller;
use Contao\CoreBundle\Exception\AccessDeniedException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;
use Twig\Environment;
#[Route('/contao/my-backend-route', name: BackendController::class, defaults: ['_scope' => 'backend'])]
class BackendController
{
private $twig;
private $security;
public function __construct(Environment $twig, Security $security)
{
$this->twig = $twig;
$this->security = $security;
}
public function __invoke(): Response
{
if (!$this->security->isGranted('ROLE_ADMIN') && !$this->security->isGranted('contao_user.my_permissions', 'first_permission')) {
throw new AccessDeniedException('Not enough permissions to access this controller.');
}
return new Response($this->twig->render('my_backend_route.html.twig', []));
}
}
Mein Ziel ist es eine Anfrage aus dem Frontend zu bearbeiten.
Ich verwende axios um eine Request an meine Route zu senden. Mein Controller sieht momentan so aus:
Code:
<?php
// src/Controller/ExampleController.php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;
#[Route('/example', name: ExampleController::class)]
class ExampleController
{
private $security;
public function __construct(Security $security)
{
//
}
public function __invoke(Request $request): Response
{
return new Response('Der Controller funktioniert!');
}
}
Ab dem Moment gab es die Fehlermeldung:
The controller for URI "/example" is not callable: Controller "App\Controller\ExampleController" cannot be fetched from the container because it is private.
Demnach habe ich in /config/services.yaml folgenden Part hinzugefügt:
Code:
services:
App\Controller\ExampleController:
public: true
tags: ['controller.service_arguments']
Nach der Korrektur kommt folgender Fehler, bei dem ich nicht mehr weiter komme.
Too few arguments to function App\Controller\ExampleController::__construct(), 0 passed in /html/contao/var/cache/dev/ContainerQpLWJJx/getExampleControllerService.php on line 22 and exactly 1 expected (500 Internal Server Error)
Der Security Helper in der Dokumentation scheint nützlich zu sein, aber ich weiss noch nicht wie man es anwendet:
https://docs.contao.org/dev/referenc...ecurity-helper
Nun zu meiner Frage. Wie kann ich eine Anfrage von einem eingeloggten User im Frontend, die an einen Controller mittels JavaScript an die URL /example geht (das Routing funktioniert), die Parameter (Security $security) im __construct verwenden? Was mache ich falsch.
Ich freue mich auf eure Antworten.
Beste Grüße, Chris
Lesezeichen