This course is still being released! Check back later for more chapters.
Using Bundle Configuration
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
With a Subscription, click any sentence in the script to jump to that part of the video!
Login SubscribeIn our bundle, we've defined and validated the translation_class
configuration option. In our app, we've configured it as App\Entity\Translation
. Now, we need to use it in our bundle's ObjectTranslator
service.
ObjectTranslator
Update
Open this class up, and add it to the constructor as private string $translationClass
:
// ... lines 1 - 6 | |
final class ObjectTranslator | |
{ | |
public function __construct( | |
// ... lines 10 - 11 | |
private string $translationClass, | |
) { | |
} | |
// ... lines 15 - 34 | |
} |
Hope over to the browser and click an article. An error: "Too few arguments to ... ObjectTranslator::__construct(), 2 passed... 3 expected".
abstract_arg()
Let's fix this. Open our bundle's config/services.php
. We need to add it as a third element in this args()
array. But... we don't have access to our configuration here. For now, we can stub it out with a special function: abstract_arg()
. For the first argument, describe its purpose: "Translation class":
// ... lines 1 - 6 | |
return static function (ContainerConfigurator $container): void { | |
$container->services() | |
->set('symfonycasts.object_translator', ObjectTranslator::class) | |
->args([ | |
// ... lines 11 - 12 | |
abstract_arg('translation class') | |
]) | |
// ... lines 15 - 16 | |
; | |
}; |
This isn't strictly required, but it helps with debugging. We're basically letting Symfony know that we need to configure this argument still.
Back in the browser, refresh. A new error: "Argument 3 of service ...object_translator is abstract". And we see our description: "Translation class". Perfect!
Using Bundle Configuration
Now... we need to find the place where we have access to the bundle configuration.
In ObjectTranslationBundle::loadExtension()
, notice the array $config
argument. Inside this method, dump it with: dd($config)
:
// ... lines 1 - 11 | |
final class ObjectTranslationBundle extends AbstractBundle | |
{ | |
// ... lines 14 - 40 | |
public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void | |
{ | |
dd($config); | |
// ... lines 44 - 45 | |
} | |
} |
Back in the browser, refresh.
Nice! Here's our processed configuration as an array! Time to put it to use!
Below the import, write $builder->getDefinition()
. Be sure to use getDefinition()
, not get()
. Inside, pass the service ID for our ObjectTranslator
service: symfonycasts.object_translator
:
// ... lines 1 - 11 | |
final class ObjectTranslationBundle extends AbstractBundle | |
{ | |
// ... lines 14 - 40 | |
public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void | |
{ | |
// ... lines 43 - 44 | |
$builder->getDefinition('symfonycasts.object_translator') | |
// ... line 46 | |
; | |
} | |
} |
One of the bonuses of working on your bundle within an app is that you can get PhpStorm + Symfony Plugin auto-completion for your services.
Next, chain ->setArgument()
. The first argument here is the 0-based index of the constructor argument we want to set. Quickly jump to ObjectTranslator
, $translationClass
is the third argument, so we need to pass 2
.
The second argument is the value we want to set: $config['translation_class']
:
// ... lines 1 - 11 | |
final class ObjectTranslationBundle extends AbstractBundle | |
{ | |
// ... lines 14 - 40 | |
public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void | |
{ | |
// ... lines 43 - 44 | |
$builder->getDefinition('symfonycasts.object_translator') | |
->setArgument(2, $config['translation_class']) | |
; | |
} | |
} |
Jump back to your browser and refresh. Nice! No errors.
Let's make sure this was given the value we expect. Open ArticleController::show()
and dd($translator)
:
// ... lines 1 - 11 | |
final class ArticleController extends AbstractController | |
{ | |
// ... lines 14 - 22 | |
'/news/{slug:article}', name: 'app_article_show') | (|
public function show(Article $article, ObjectTranslator $translator): Response | |
{ | |
dd($translator); | |
// ... lines 27 - 37 | |
} | |
} |
Back in the browser... refresh. Sweet! Here's our ObjectTranslator
object and indeed, the translationClass
property is set to App\Entity\Translation
.
Remove the dd()
, refresh, and we're ready to move on!
Next, let's start writing our translation logic in ObjectTranslator
.