Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.3k
More "id" => type hints / autowiring changes#7883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -24,8 +24,8 @@ which represents the functionality that you'll expose in your SOAP service. | ||
| In this case, the SOAP service will allow the client to call a method called | ||
| ``hello``, which happens to send an email:: | ||
| // src/AppBundle/Service/HelloService.php | ||
| namespaceAppBundle\Service; | ||
| class HelloService | ||
| { | ||
| @@ -50,56 +50,72 @@ In this case, the SOAP service will allow the client to call a method called | ||
| } | ||
| } | ||
| Next, make sure that your new class is registered as a service. If you use | ||
| :doc:`autowiring </service_container/autowiring>` (enabled by default in the Symfony | ||
| Standard Edition), this is easy: | ||
| .. configuration-block:: | ||
| .. code-block:: yaml | ||
| # app/config/services.yml | ||
| services: | ||
| _defaults: | ||
| # ... be sure autowiring is enabled | ||
| autowire: true | ||
| # ... | ||
| # add Service/ to the list of directories to load services from | ||
| AppBundle\: | ||
| resource: '../../src/AppBundle/{Service,Updates,Command,Form,EventSubscriber,Twig,Security}' | ||
| .. code-block:: xml | ||
| <!-- app/config/services.xml --> | ||
| <?xml version="1.0" encoding="UTF-8" ?> | ||
| <container xmlns="http://symfony.com/schema/dic/services" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://symfony.com/schema/dic/services | ||
| http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
| <services> | ||
| <!-- ... be sure autowiring is enabled --> | ||
| <defaults autowire="true" ... /> | ||
| <!-- ... --> | ||
| <!-- add Service/ to the list of directories to load services from --> | ||
| <prototype namespace="AppBundle\" resource="../../src/AppBundle/{Service,Updates,Command,Form,EventSubscriber,Twig,Security}" /> | ||
| </services> | ||
| </container> | ||
| .. code-block:: php | ||
| // app/config/services.php | ||
| useAppBundle\Service\HelloService; | ||
| $container->autowire(HelloService::class) | ||
| ->setPublic(false); | ||
| Below is an example of a controller that is capable of handling a SOAP | ||
| request. Because ``indexAction()`` is accessible via ``/soap``, the WSDL document | ||
| can be retrieved via ``/soap?wsdl``:: | ||
| namespaceAppBundle\Controller; | ||
| use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
| use Symfony\Component\HttpFoundation\Response; | ||
| use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | ||
| use AppBundle\Service\HelloService; | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Shouldn't use statements be ordered ? MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I actually don't know, I didn't see anything (looked quickly) in the docs contributor page, but I could be wrong Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Me neither but I often see them ordered in symfony's code. MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Ah, theydo follow that standard in core. I suppose we could do, but I'll worry about that later ;) | ||
| class HelloServiceController extends Controller | ||
| { | ||
| /** | ||
| * @Route("/soap") | ||
| */ | ||
| public function indexAction(HelloService $helloService) | ||
| { | ||
| $server = new \SoapServer('/path/to/hello.wsdl'); | ||
| $server->setObject($helloService); | ||
| $response = new Response(); | ||
| $response->headers->set('Content-Type', 'text/xml; charset=ISO-8859-1'); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -221,8 +221,8 @@ Creating an Uploader Service | ||
| To avoid logic in controllers, making them big, you can extract the upload | ||
| logic to a separate service:: | ||
| // src/AppBundle/Service/FileUploader.php | ||
| namespace AppBundle\Service; | ||
| use Symfony\Component\HttpFoundation\File\UploadedFile; | ||
| @@ -259,47 +259,51 @@ Then, define a service for this class: | ||
| # app/config/services.yml | ||
| services: | ||
| # ... | ||
| AppBundle\Service\FileUploader: | ||
| arguments: | ||
| $targetDir: '%brochures_directory%' | ||
| .. code-block:: xml | ||
| <!-- app/config/services.xml --> | ||
| <?xml version="1.0" encoding="UTF-8" ?> | ||
| <container xmlns="http://symfony.com/schema/dic/services" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://symfony.com/schema/dic/services | ||
| http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
| <services> | ||
| <!-- ... --> | ||
| <service id="AppBundle\Service\FileUploader"> | ||
| <argument key="$targetDir">%brochures_directory%</argument> | ||
| </service> | ||
| </services> | ||
| </container> | ||
| .. code-block:: php | ||
| // app/config/services.php | ||
| use AppBundle\Service\FileUploader; | ||
| $container->autowire(FileUploader::class) | ||
| ->setArgument('$targetDir', '%brochures_directory%'); | ||
| Now you're ready to use this service in the controller:: | ||
| // src/AppBundle/Controller/ProductController.php | ||
| use Symfony\Component\HttpFoundation\Request; | ||
| use AppBundle\Service\FileUploader; | ||
| // ... | ||
| public function newAction(Request $request, FileUploader $fileUploader) | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. What about adding a use statement for | ||
| { | ||
| // ... | ||
| if ($form->isSubmitted() && $form->isValid()) { | ||
| $file = $product->getBrochure(); | ||
| $fileName = $fileUploader->upload($file); | ||
| $product->setBrochure($fileName); | ||
| @@ -323,7 +327,7 @@ automatically upload the file when persisting the entity:: | ||
| use Doctrine\ORM\Event\LifecycleEventArgs; | ||
| use Doctrine\ORM\Event\PreUpdateEventArgs; | ||
| use AppBundle\Entity\Product; | ||
| use AppBundle\Service\FileUploader; | ||
| class BrochureUploadListener | ||
| { | ||
| @@ -375,10 +379,12 @@ Now, register this class as a Doctrine listener: | ||
| # app/config/services.yml | ||
| services: | ||
| _defaults: | ||
| # ... be sure autowiring is enabled | ||
| autowire: true | ||
| # ... | ||
| AppBundle\EventListener\BrochureUploadListener: | ||
| tags: | ||
| - { name: doctrine.event_listener, event: prePersist } | ||
| - { name: doctrine.event_listener, event: preUpdate } | ||
| @@ -392,33 +398,44 @@ Now, register this class as a Doctrine listener: | ||
| xsi:schemaLocation="http://symfony.com/schema/dic/services | ||
| http://symfony.com/schema/dic/services/services-1.0.xsd" | ||
| > | ||
| <services> | ||
| <!-- ... be sure autowiring is enabled --> | ||
| <defaults autowire="true" ... /> | ||
| <!-- ... --> | ||
| <service id="AppBundle\EventListener\BrochureUploaderListener"> | ||
| <tag name="doctrine.event_listener" event="prePersist"/> | ||
| <tag name="doctrine.event_listener" event="preUpdate"/> | ||
| </service> | ||
| </services> | ||
| </container> | ||
| .. code-block:: php | ||
| // app/config/services.php | ||
| use AppBundle\EventListener\BrochureUploaderListener; | ||
| <<<<<<< HEAD | ||
| use Symfony\Component\DependencyInjection\Reference; | ||
| // ... | ||
| $container->register('app.doctrine_brochure_listener', BrochureUploaderListener::class) | ||
| ->addArgument(new Reference('brochures_directory')) | ||
| ======= | ||
| $container->autowire(BrochureUploaderListener::class) | ||
| >>>>>>> 8974c4842... Going through more chapters to use types and autowiring | ||
| ->addTag('doctrine.event_listener', array( | ||
| 'event' => 'prePersist', | ||
| )) | ||
| ->addTag('doctrine.event_listener', array( | ||
| <<<<<<< HEAD | ||
| 'event' => 'prePersist', | ||
| )); | ||
| ======= | ||
| 'event' => 'preUpdate', | ||
| )) | ||
| ; | ||
| >>>>>>> 8974c4842... Going through more chapters to use types and autowiring | ||
| This listener is now automatically executed when persisting a new Product | ||
| entity. This way, you can remove everything related to uploading from the | ||
Uh oh!
There was an error while loading.Please reload this page.