-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[DI] Documenting Abstract Bundle and Extension #16801
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
Changes from 4 commits
426e289
c93db0b
c2c47a2
d0cba72
7cf9bf4
558b02e
c85de08
c426dbb
7a24f08
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 |
---|---|---|
|
@@ -99,8 +99,8 @@ that define your bundles, your services and your routes: | |
``RoutingConfigurator`` has methods that make adding routes in PHP more | ||
fun. You can also load external routing files (shown below). | ||
|
||
Advanced Example: Twig, Annotations and the Web Debug Toolbar | ||
------------------------------------------------------------- | ||
Advanced Example: Configuration, Twig, Annotations and the Web Debug Toolbar | ||
---------------------------------------------------------------------------- | ||
|
||
The purpose of the ``MicroKernelTrait`` is *not* to have a single-file application. | ||
Instead, its goal to give you the power to choose your bundles and structure. | ||
|
@@ -123,13 +123,15 @@ your ``composer.json`` file to load from there: | |
|
||
Then, run ``composer dump-autoload`` to dump your new autoload config. | ||
|
||
Now, suppose you want to use Twig and load routes via annotations. Instead of | ||
putting *everything* in ``index.php``, create a new ``src/Kernel.php`` to | ||
hold the kernel. Now it looks like this:: | ||
Now, suppose you want to define a custom configuration for your app, | ||
use Twig and load routes via annotations. Instead of putting *everything* | ||
in ``index.php``, create a new ``src/Kernel.php`` to hold the kernel. | ||
Now it looks like this:: | ||
|
||
// src/Kernel.php | ||
namespace App; | ||
|
||
use App\DependencyInjection\AppExtension; | ||
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
use Symfony\Component\HttpKernel\Kernel as BaseKernel; | ||
|
@@ -146,13 +148,18 @@ hold the kernel. Now it looks like this:: | |
new \Symfony\Bundle\TwigBundle\TwigBundle(), | ||
]; | ||
|
||
if ($this->getEnvironment() == 'dev') { | ||
if ('dev' === $this->getEnvironment()) { | ||
$bundles[] = new \Symfony\Bundle\WebProfilerBundle\WebProfilerBundle(); | ||
} | ||
|
||
return $bundles; | ||
} | ||
|
||
protected function build(ContainerBuilder $container) | ||
{ | ||
$container->registerExtension(new AppExtension()); | ||
} | ||
|
||
protected function configureContainer(ContainerConfigurator $c): void | ||
{ | ||
$c->import(__DIR__.'/../config/framework.yaml'); | ||
|
@@ -205,6 +212,39 @@ Before continuing, run this command to add support for the new dependencies: | |
|
||
$ composer require symfony/yaml symfony/twig-bundle symfony/web-profiler-bundle doctrine/annotations | ||
|
||
Next, create a new extension class that defines your app configuration and | ||
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. To be honest, I'm not really sure if this is a good place to document If we want to keep it, I would suggest moving the changes in this document to a new section below this one. Currently, I'm afraid the doc can make people think that they must use the app extension (or 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.
Apps on bundle-less approach still need to define custom configuration, and it's only possible through a new extension, even if the kernel class can implement the ExtensionInterface + Configuration it's better now by creating a new extension class and extending from AbstractExtension.
I think you're right, it could be better in a new section with the proper explanation. |
||
add a service conditionally based on the ``foo`` value:: | ||
|
||
// src/DependencyInjection/AppExtension.php | ||
namespace App\DependencyInjection; | ||
|
||
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Extension\AbstractExtension; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
class AppExtension extends AbstractExtension | ||
yceruto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
public function configure(DefinitionConfigurator $definition): void | ||
{ | ||
$definition->rootNode() | ||
->children() | ||
->booleanNode('foo')->defaultTrue()->end() | ||
->end(); | ||
} | ||
|
||
public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void | ||
{ | ||
if ($config['foo']) { | ||
$container->set('foo_service', new \stdClass()); | ||
} | ||
} | ||
} | ||
|
||
.. versionadded:: 6.1 | ||
|
||
The ``AbstractExtension`` class is introduced in Symfony 6.1. | ||
|
||
Unlike the previous kernel, this loads an external ``config/framework.yaml`` file, | ||
because the configuration started to get bigger: | ||
|
||
|
@@ -257,9 +297,7 @@ has one file in it:: | |
|
||
class MicroController extends AbstractController | ||
{ | ||
/** | ||
* @Route("/random/{limit}") | ||
*/ | ||
#[Route('/random/{limit}')] | ||
yceruto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public function randomNumber(int $limit): Response | ||
{ | ||
$number = random_int(0, $limit); | ||
|
Uh oh!
There was an error while loading. Please reload this page.