website/errors/symfonyContainer.serviceNotFound.md
<?php declare(strict_types = 1);
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class MyController extends AbstractController
{
public function index(): void
{
$this->get('unknown_service');
}
}
The service identifier passed to ContainerInterface::get() does not match any service registered in the Symfony dependency injection container. This means the call will throw a ServiceNotFoundException at runtime.
PHPStan reads the compiled container configuration to know which services are available. If the service ID is not found in the compiled container, this error is reported.
This rule is provided by the phpstan-symfony package.
Use a valid service identifier that is registered in the container:
<?php declare(strict_types = 1);
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class MyController extends AbstractController
{
public function index(): void
{
- $this->get('unknown_service');
+ $this->get('app.my_service');
}
}
Alternatively, use constructor injection instead of fetching services from the container directly, which is the recommended approach in modern Symfony applications:
<?php declare(strict_types = 1);
namespace App\Controller;
use App\Service\MyService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class MyController extends AbstractController
{
- public function index(): void
+ public function __construct(private MyService $myService)
{
- $service = $this->get('app.my_service');
+ }
+
+ public function index(): void
+ {
+ $this->myService->doSomething();
}
}