简体   繁体   中英

Resolving Controller Services in Sylius/Symfony

Hoes does Symfony resolve the Sylius service sylius.controller.shop_user service to the controller class file Sylius\\Bundle\\UserBundle\\Controller\\UserController .

My understanding is that sylius.controller.shop_user is a service, and that in Symfony there will be a corresponding service configuration. This service configuration will tell Symfony which class to use when it needs to instantiate the service.

However, I can't seem to find a sylius.controller.shop_user configuration in the Sylius source configuration anywhere. There's just references to this service in routing files

#File: src/Sylius/Bundle/ShopBundle/Resources/config/routing/ajax/user.yml
sylius_shop_ajax_user_check_action:
    path: /check
    methods: [GET]
    defaults:
        _controller: sylius.controller.shop_user:showAction
        _format: json
        _sylius:
            repository:
                method: findOneByEmail
                arguments:
                    email: $email
            serialization_groups: [Secured]

or in on-disk container cache files.

var/cache/dev/srcKernelDevDebugContainer.xml
1798:    <parameter key="sylius.controller.shop_user.class">Sylius\Bundle\UserBundle\Controller\UserController</parameter>
15230:    <service id="sylius.controller.shop_user" class="Sylius\Bundle\UserBundle\Controller\UserController" public="true">

So how does Symfony know to instantiate the right class for this service?

Is there configuration I'm not seeing? Some Symfony magic that auto-generates the class? Some other mysterious third thing where I don't know what I don't know?

I don't have any specific task in mind, I'm just trying to get a feel for how Sylius and Symfony work under the hood.

The controller service is defined based on ResourceBundle's configuration in Sylius\\Bundle\\ResourceBundle\\DependencyInjection\\Driver\\AbstractDriver::addController . This driver is called when loading a bundle.

Services with the name sylius.controller.[entity-name] are part of the Sylius entity resource system . As best I can tell, when you define your new doctrine entities in a specific way and register them as a Sylius resource , Sylius will automatically generate these controller services based on your configuration.

The actual line of code that defines these services is here .

#File: src/Sylius/Bundle/ResourceBundle/DependencyInjection/Driver/AbstractDriver.php
/* ... */
$container->setDefinition($metadata->getServiceId('controller'), $definition);
/* ... */

The Sylius\\Bundle\\ResourceBundle\\DependencyInjection\\Driver\\AbstractDriver class is a (as of 1.3) a base class for the Sylius\\Bundle\\ResourceBundle\\DependencyInjection\\Driver\\Doctrine\\DoctrineORMDriver class. How this class ends up being used is by Symfony is unclear, but is fortunately beyond the scope of this answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM