Ergebnis 1 bis 3 von 3

Thema: Controller initialize framework

  1. #1
    Contao-Nutzer
    Registriert seit
    06.08.2010.
    Beiträge
    82

    Standard Controller initialize framework

    Hallo,

    ich versuche, contao innerhalb eines Controllers zu initialisieren

    Code:
    /src/Controller/AppController.php
    Das sieht dann so aus:

    PHP-Code:
    <?php
    namespace App\Controller;

    use 
    Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
    use 
    Symfony\Component\HttpFoundation\Response;
    use 
    Symfony\Component\HttpKernel\Attribute\AsController;
    use 
    Symfony\Component\Routing\Annotation\Route;
    use 
    Contao\CoreBundle\Framework\ContaoFramework;
    use 
    Contao\MemberModel;

    #[AsController]
    class AppController extends AbstractController
    {
        
    #[Route(
            
    path'/myroute',
            
    name'myroute',
            
    defaults: ['_scope' => 'frontend'],
        )]
        public function 
    getExperts(): Response
        
    {
            
            
    $this->container->get('contao.framework')->initialize();

        }
    }
    Ich bekomme dann folgende Fehlermeldung:

    Code:
    [2025-05-02T11:09:39.493586+00:00] request.CRITICAL: Uncaught PHP Exception Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: "Service "contao.framework" not found: even though it exists in the app's container, the container inside "App\Controller\AppController" is a smaller service locator that only knows about the "http_kernel", "parameter_bag", "request_stack", "router", "security.authorization_checker", "security.csrf.token_manager", "security.token_storage" and "twig" services. Try using dependency injection instead." at ServiceLocator.php line 133 {"exception":"[object] (Symfony\\Component\\DependencyInjection\\Exception\\ServiceNotFoundException(code: 0): Service \"contao.framework\" not found: even though it exists in the app's container, the container inside \"App\\Controller\\AppController\" is a smaller service locator that only knows about the \"http_kernel\", \"parameter_bag\", \"request_stack\", \"router\", \"security.authorization_checker\", \"security.csrf.token_manager\", \"security.token_storage\" and \"twig\" services. Try using dependency injection instead. at /home/benfolds/dgfm-website/vendor/symfony/dependency-injection/ServiceLocator.php:133)"} {"request_uri":"URI","request_method":"GET"}
    Wie ist der richtige Weg? Vermutlich fehlt nur eine Kleinigkeit, um den container ordentlich zu laden?
    Danke!

  2. #2
    Community-Moderator
    Wandelndes Contao-Lexikon
    Avatar von Spooky
    Registriert seit
    12.04.2012.
    Ort
    Scotland
    Beiträge
    37.158
    Partner-ID
    10107

    Standard

    Du extendest vom Symfony AbstractController - dieser AbstractController subscribed ja nicht zum contao.framework Service. Du könntest vom Contao AbstractController stattdessen extenden - denn dieser subscribed zum contao.framework Service. Oder du subscribest zu diesem Service selbst in deinem Controller. Oder du nutzt normale Dependency Injection via Constructor.
    » sponsor me via GitHub or Revolut

  3. #3
    Contao-Nutzer
    Registriert seit
    06.08.2010.
    Beiträge
    82

    Standard

    Das macht so viel Sinn
    Danke!

    Für die Akten:
    PHP-Code:
    use Contao\CoreBundle\Controller\AbstractController

Aktive Benutzer

Aktive Benutzer

Aktive Benutzer in diesem Thema: 1 (Registrierte Benutzer: 0, Gäste: 1)

Berechtigungen

  • Neue Themen erstellen: Nein
  • Themen beantworten: Nein
  • Anhänge hochladen: Nein
  • Beiträge bearbeiten: Nein
  •