Service not found the container inside “ServiceLocator” is a smaller service locator that only knows about the “kernel”

Because I want my applications to follow some kind of DDD structure, I want my Controller, Entity, Repository, Form etc to sit within a specific directory. For example: src/Appication/Dealer/Controller/DealerController.php

In order to load this controller, i have created a routing service: src/Routing/ApplicationLoader.php. That works fine if i run it in a test script. It gives me a list of the routes that are available within the dealer controller.

But symfony is giving me this:

Service "@AppRoutingApplicationLoader" not found: the container inside "SymfonyComponentDependencyInjectionArgumentServiceLocator" is a smaller service locator that only knows about the "kernel" and "security.route_loader.logout" services in @AppRoutingApplicationLoader (which is being imported from "/var/www/html/config/routes.yaml"). Make sure the "AppRoutingApplicationLoader" bundle is correctly registered and loaded in the application kernel class. If the bundle is registered, make sure the bundle path "@AppRoutingApplicationLoader" is not empty.

It’s not even hitting the ApplicationLoader.php (tested by adding an exit e.g.)

How can i get symfony to understand it needs to load my script? 😉

(and i think my routes and services are correct – see below)

This is my services.yaml

parameters:

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: false      # Automatically injects dependencies in your services.
        autoconfigure: false # Automatically registers your services as commands, event subscribers, etc.

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App:
        resource: '../src/'
        exclude:
            - '../src/DependencyInjection/'
            - '../src/Entity/'
            - '../src/Kernel.php'

    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones
    AppRoutingApplicationLoader:
        public: true
        arguments:
            $controllerDirectory: '%kernel.project_dir%/src/Application'
        tags:
            - { name: 'router.loader', priority: 0 }

This is my routes.yaml

# Handles routes for generic controllers in src/Controller/

controllers:
resource:
path: ../src/Controller/
namespace: App\Controller
type: attribute

# Handles custom routes for controllers under src/Application

custom_routes:
resource: '@App\Routing\ApplicationLoader'
type: service

This is my ApplicationLoader.php:

<?php

namespace App\Routing;

use SymfonyComponentConfigLoaderLoader;
use SymfonyComponentRoutingRoute;
use SymfonyComponentRoutingRouteCollection;
use SymfonyComponentFinderFinder;
use ReflectionClass;

class ApplicationLoader extends Loader
{
private bool $isLoaded = false;

    private string $controllerDirectory;
    
    public function __construct(string $controllerDirectory)
    {
        $this->controllerDirectory = $controllerDirectory;
    }
    
    public function load($resource, ?string $type = null): RouteCollection
    {
    
        error_log("Loading resource: $resource with type: $type");
        if (true === $this->isLoaded) {
            throw new RuntimeException('Do not add the "application" loader twice');
        }
    
        $routes = new RouteCollection();
    
        // Use Symfony Finder to scan the directory for controllers
        $finder = new Finder();
        $finder->files()->in($this->controllerDirectory)->name('*Controller.php');
    
        // Iterate through controller files and add routes
        foreach ($finder as $file) {
            $className = $this->getClassNameFromFile($file);
            error_log("Processing controller: $className");
    
            $reflectionClass = new ReflectionClass($className);
    
            // Check if the class has route annotations or attributes
            if ($reflectionClass->isSubclassOf('SymfonyBundleFrameworkBundleControllerAbstractController')) {
                $this->addRoutesFromAttributes($reflectionClass, $routes);
            }
        }
    
        return $routes;
    }
    
    public function supports($resource, ?string $type = null): bool
    {
        error_log("Checking support for resource: $resource with type: $type");
    
        return 'service' === $type;
    }
    
    private function getClassNameFromFile($file): string
    {
        // Resolve the absolute path of the controller directory
        $controllerDirectoryRealPath = realpath($this->controllerDirectory);
    
        // Resolve the file's real path
        $fileRealPath = $file->getRealPath();
    
        // Calculate the relative path by removing the base directory
        $relativePath = substr($fileRealPath, strlen($controllerDirectoryRealPath) + 1);
    
        // Normalize directory separators to backslashes for namespaces
        $relativePath = str_replace(DIRECTORY_SEPARATOR, '\', $relativePath);
    
        // Remove the file extension (.php) and prepend the base namespace
        return 'App\Application\' . str_replace('.php', '', $relativePath);
    }
    
    private function addRoutesFromAttributes(ReflectionClass $reflectionClass, RouteCollection $routes): void
    {
        // Check the class-level attributes for the base path (e.g., #[Route('/dealer')])
        $classAttributes = $reflectionClass->getAttributes(SymfonyComponentRoutingAttributeRoute::class);
        $classBasePath = '';
    
        foreach ($classAttributes as $classAttribute) {
            $classRoute = $classAttribute->newInstance();
            $classBasePath = $classRoute->getPath();
        }
    
        // Iterate through each method to find route attributes
        foreach ($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
            // Ignore inherited methods from AbstractController
    
            // Skip methods from parent classes
            if ($method->getDeclaringClass()->getName() !== $reflectionClass->getName()) {
                continue;
            }
    
            // Get method attributes for routes
            $methodAttributes = $method->getAttributes(SymfonyComponentRoutingAttributeRoute::class);
    
            foreach ($methodAttributes as $methodAttribute) {
    
                $methodRoute = $methodAttribute->newInstance();
    
                // Combine the class-level base path and the method-level path
                $fullPath = rtrim($classBasePath, '/') . '/' . ltrim($methodRoute->getPath() ?? '', '/');
    
                // Add the route to the collection
                $routes->add(
                    $methodRoute->getName(),
                    new Route(
                        $fullPath,
                        ['_controller' => $reflectionClass->getName() . '::' . $method->getName()],
                        [],        // Default requirements
                        [],        // Default options
                        '',        // Host
                        [],        // Schemes
                        $methodRoute->getMethods()
                    )
                );
            }
        }
    }

}

this is my test.php script that lives in /public

<?php

require_once __DIR__ . '/../vendor/autoload.php';

use App\Routing\ApplicationLoader;
use Symfony\Component\Routing\RouteCollection;

try {
// Define the directory where your Application controllers are located
$controllerDirectory = __DIR__ . '/../src/Application';

    // Instantiate your ApplicationLoader with the specified directory
    $loader = new ApplicationLoader($controllerDirectory);
    
    // Invoke the loader to load routes
    $routes = $loader->load(null, 'service');
    
    // Display loaded routes
    echo "Loaded Routes:n";
    foreach ($routes as $name => $route) {
        echo " - $name: " . $route->getPath() . "<br/>";
        echo "   Controller: " . $route->getDefault('_controller') . "<br/>";
        echo "   Methods: " . implode(', ', $route->getMethods()) . "<br/><br/>";
    }

} catch (Throwable $e) {
// Catch and display errors for debugging
echo "Error: " . $e->getMessage() . "<br/>";
echo $e->getTraceAsString() . "<br/>";
}

Please help 😉