|
1 | 1 | The Architecture
|
2 | 2 | ================
|
3 | 3 |
|
4 |
| -TODO |
| 4 | +You are my hero! Who would have thought that you would still be here after the first |
| 5 | +two parts? Your efforts will be well-rewarded soon. The first two parts didn't look |
| 6 | +too deeply at the architecture of the framework. Because it makes Symfony stand apart |
| 7 | +from the framework crowd, let's dive into the architecture now. |
| 8 | + |
| 9 | +Add Logging |
| 10 | +----------- |
| 11 | + |
| 12 | +A new Symfony app is micro: it's basically just a routing & controller system. But |
| 13 | +thanks to Flex, installing more features is simple. |
| 14 | + |
| 15 | +Want a logging system? No problem: |
| 16 | + |
| 17 | +.. code-block:: terminal |
| 18 | +
|
| 19 | + $ composer require logger |
| 20 | +
|
| 21 | +This installs and configures (via a recipe) the powerful `Monolog`_ library. To |
| 22 | +use the logger in a controller, add a new argument type-hinted with ``LoggerInterface``: |
| 23 | + |
| 24 | + use Psr\Log\LoggerInterface; |
| 25 | + // ... |
| 26 | + |
| 27 | + public function index($name, LoggerInterface $logger) |
| 28 | + { |
| 29 | + $logger->info("Saying hello to $name!"); |
| 30 | + |
| 31 | + // ... |
| 32 | + } |
| 33 | + |
| 34 | +That's it! The new log message be written to ``var/log/dev.log``. Of course, this |
| 35 | +can be configured by updating one of the config files added by the recipe. |
| 36 | + |
| 37 | +Services & Autowiring |
| 38 | +--------------------- |
| 39 | + |
| 40 | +But wait! Something *very* cool just happened. Symfony read the ``LoggerInterface`` |
| 41 | +type-hint and automatically figured out that it should pass us the Logger object! |
| 42 | +This is called *autowiring*. |
| 43 | + |
| 44 | +Every bit of work that's done in a Symfony app is done by an *object*: the Logger |
| 45 | +object logs things and the Twig object renders templates. These objects are called |
| 46 | +*services* and they are the *tools* that make you dangerous. |
| 47 | + |
| 48 | +To make life awesome, you can ask Symfony to pass you a service by using a type-hint. |
| 49 | +What other possible classes or interfaces could you use? Find out by running: |
| 50 | + |
| 51 | +.. code-block:: terminal |
| 52 | +
|
| 53 | + $ php bin/console debug:autowiring |
| 54 | +
|
| 55 | +=============================================================== ===================================== |
| 56 | +Class/Interface Type Alias Service ID |
| 57 | +=============================================================== ===================================== |
| 58 | +``Psr\Cache\CacheItemPoolInterface`` alias for "cache.app.recorder" |
| 59 | +``Psr\Log\LoggerInterface`` alias for "monolog.logger" |
| 60 | +``Symfony\Component\EventDispatcher\EventDispatcherInterface`` alias for "debug.event_dispatcher" |
| 61 | +``Symfony\Component\HttpFoundation\RequestStack`` alias for "request_stack" |
| 62 | +``Symfony\Component\HttpFoundation\Session\SessionInterface`` alias for "session" |
| 63 | +``Symfony\Component\Routing\RouterInterface`` alias for "router.default" |
| 64 | +=============================================================== ===================================== |
| 65 | + |
| 66 | +This is just a short summary of the full list! And as you add more packages, this |
| 67 | +list of tools will grow! |
| 68 | + |
| 69 | +Creating Services |
| 70 | +----------------- |
| 71 | + |
| 72 | +To keep your code organized, you can even create your own services! Suppose you |
| 73 | +want to generate a random greeting (e.g. "Hello", "Yo", etc). Instead of putting |
| 74 | +this code directly in your controller, create a new class: |
| 75 | + |
| 76 | + // src/GreetingGenerator.php |
| 77 | + namespace App; |
| 78 | + |
| 79 | + class GreetingGenerator |
| 80 | + { |
| 81 | + public function getRandomGreeting() |
| 82 | + { |
| 83 | + $greetings = ['Hey', 'Yo', 'Aloha']; |
| 84 | + $greeting = $greetings[array_rand($greetings)]; |
| 85 | + |
| 86 | + return $greeting; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | +Great! You can use this immediately in your controller:: |
| 91 | + |
| 92 | + use App\GreetingGenerator; |
| 93 | + // ... |
| 94 | + |
| 95 | + public function index($name, LoggerInterface $logger, GreetingGenerator $generator) |
| 96 | + { |
| 97 | + $greeting = $generator->getRandomGreeting(); |
| 98 | + |
| 99 | + $logger->info("Saying $greeting to $name!"); |
| 100 | + |
| 101 | + // ... |
| 102 | + } |
| 103 | + |
| 104 | +That's it! Symfony will instantiate the ``GreetingGenerator`` automatically and |
| 105 | +pass it as an argument. But, could we *also* move the logger logic to ``GreetingGenerator``? |
| 106 | +Yes! You can use autowiring inside a service to access *other* services. The only |
| 107 | +difference is that it's done in the constructor: |
| 108 | + |
| 109 | +.. code-block:: diff |
| 110 | +
|
| 111 | + + use Psr\Log\LoggerInterface; |
| 112 | +
|
| 113 | + class GreetingGenerator |
| 114 | + { |
| 115 | + + private $logger; |
| 116 | + + |
| 117 | + + public function __construct(LoggerInterface $logger) |
| 118 | + + { |
| 119 | + + $this->logger = $logger; |
| 120 | + + } |
| 121 | +
|
| 122 | + public function getRandomGreeting() |
| 123 | + { |
| 124 | + // ... |
| 125 | +
|
| 126 | + + $this->logger->info('Using the greeting: '.$greeting); |
| 127 | +
|
| 128 | + return $greeting; |
| 129 | + } |
| 130 | + } |
| 131 | +
|
| 132 | +Yes! This works too: no configuration, no time wasted. |
| 133 | + |
| 134 | +Event Subscriber |
| 135 | +---------------- |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | +.. _`Monolog`: https://github.com/Seldaek/monolog |
0 commit comments