Skip to content

[DX] ADR usage #8153

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 29 commits into from
Closed
Changes from 10 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c8050b1
Adding the ADR approach.
Guikingone Jul 14, 2017
0d0d4cc
Update service.rst
Guikingone Jul 14, 2017
7359837
Update service.rst
Guikingone Jul 14, 2017
885e696
Update service.rst
Guikingone Jul 14, 2017
220021e
Update service.rst
Guikingone Jul 14, 2017
1fda263
Update service.rst
Guikingone Jul 14, 2017
11c77c7
Update service.rst
Guikingone Jul 14, 2017
d21feb2
Update service.rst
Guikingone Jul 15, 2017
a449220
Update service.rst
Guikingone Jul 17, 2017
6d1ed62
Update service.rst
Guikingone Jul 17, 2017
a81b625
Update service.rst
Guikingone Aug 3, 2017
8444e98
Added the dedicated file and link from service.rst
Guikingone Aug 3, 2017
a93fa6c
Update adr.rst
Guikingone Aug 3, 2017
97bcbac
Update adr.rst
Guikingone Aug 3, 2017
72c2c74
Update service.rst
Guikingone Aug 3, 2017
666a89f
Update adr.rst
Guikingone Aug 3, 2017
c8625fb
Update adr.rst
Guikingone Aug 5, 2017
0d2022a
Corrected the index typo.
Guikingone Aug 5, 2017
582f4bf
Update adr.rst
Guikingone Aug 5, 2017
a237312
Update adr.rst
Guikingone Aug 6, 2017
cd46501
Update adr.rst
Guikingone Aug 16, 2017
24416f5
Update adr.rst
Guikingone Sep 12, 2017
dc6ddc6
Update adr.rst
Guikingone Oct 16, 2017
f1aad20
Update adr.rst
Guikingone Oct 16, 2017
16c8472
Update adr.rst
Guikingone Oct 16, 2017
eca9a0a
[ADD](Responder classe)[!P2]
Guikingone Nov 24, 2017
aa0a207
[FIX](Syntax)[!P2]
Guikingone Nov 24, 2017
551aed6
[FIX](ADR Repository)[!P2]
Guikingone Nov 24, 2017
3dacc68
fix(typo): fix on typo and space.
Guikingone Jan 24, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions controller/service.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,98 @@ If your controller implements the ``__invoke()`` method - popular with the
Action-Domain-Response (ADR) pattern, you can simply refer to the service id
(``AppBundle\Controller\HelloController`` or ``app.hello_controller`` for example).

If you want to use the ADR pattern rather than the default controller approach, you can use
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this deserves its own article to be referenced here.

the new features provided by the container like public/private injections and autowiring,
in order to work, you must update the services.yml file ::

# ...

services:
_defaults:
autowire: true
autoconfigure: true
public: false

# Allow to load every actions
AppBundle\Action\:
resource: '../../src/AppBundle/Action/'
public: true

Once the file is updated, delete your Controller folder and create an Action one then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Once the file is updated, delete your Controller folder and create an Action class using the ADR principles, i.e ::"

build a HelloAction class that use the ADR principes ::

<?php

namespace AppBundle\Action;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Templating\EngineInterface;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This component is to be deprecated, a Twig Environment should be used instead.


final class HelloAction
{
private $render;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be named $twig or $renderer.


public function __construct(EngineInterface $render)
{
$this->render = $render;
}

public function __invoke()
{
return new Response($this->render->render('default/index.html.twig'));
}
}

By default, we define the class with the final keyword because this class shouldn't been extended,
the logic is pretty simple to understand as you understand the ADR pattern, in fact, the 'Action'
is linked to a single request and his dependencies are linked to this precise Action.

Once this is done, you can define the routes like before using multiples approach :

.. configuration-block::

.. code-block:: php-annotations
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

php annotations should be the first (as recommended way)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


# src/AppBundle/Action/HelloAction.php
// ...

/**
* @Route("/hello", name="hello")
*/
final class HelloAction
{
// ...
}

.. code-block:: yaml

# app/config/routing.yml
hello:
path: /hello
defaults: { _controller: AppBundle\Action\HelloAction }

.. code-block:: xml

<!-- app/config/routing.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing
http://symfony.com/schema/routing/routing-1.0.xsd">

<route id="hello" path="/hello">
<default key="_controller">AppBundle\Action\HelloAction</default>
</route>

</routes>

.. code-block:: php

// app/config/routing.php
$collection->add('hello', new Route('/hello', array(
'_controller' => 'AppBundle\Action\HelloAction',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HelloAction::class

)));

Alternatives to base Controller Methods
---------------------------------------

Expand Down