-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[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
[DX] ADR usage #8153
Changes from 10 commits
c8050b1
0d0d4cc
7359837
885e696
220021e
1fda263
11c77c7
d21feb2
a449220
6d1ed62
a81b625
8444e98
a93fa6c
97bcbac
72c2c74
666a89f
c8625fb
0d2022a
582f4bf
a237312
cd46501
24416f5
dc6ddc6
f1aad20
16c8472
eca9a0a
aa0a207
551aed6
3dacc68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
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 | ||
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. "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; | ||
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. This component is to be deprecated, a Twig |
||
|
||
final class HelloAction | ||
{ | ||
private $render; | ||
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. Should be named |
||
|
||
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 | ||
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. php annotations should be the first (as recommended way)? 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. http://symfony.com/doc/current/contributing/documentation/standards.html#formats:
|
||
|
||
# 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', | ||
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.
|
||
))); | ||
|
||
Alternatives to base Controller Methods | ||
--------------------------------------- | ||
|
||
|
There was a problem hiding this comment.
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.