Chapters
This course is still being released! Check back later for more chapters.
-
Course Code
Subscribe to download the code!
Subscribe to download the code!
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
We have a home for our bundle within our app. And, we've set up it's composer.json file so it's marked as a package for composer. Now it needs a "bundle class" so it can be loaded as a bundle by Symfony.
This class is the heart of your bundle - the primary entry point.
In the src/ directory, create a new PHP class... name ObjectTranslationBundle. But hang on, we've got a small hiccup. The "namespace field" isn't being filled in by PhpStorm like it usually is. PhpStorm isn't yet aware of our bundle namespace.
Cancel out of this dialog for now. In our bundle's composer.json file, we set the namespace for our src/ directory to be SymfonyCasts\ObjectTranslationBundle\.
Copy the namespace. We need to help PhpStorm a bit here.
Updating Your Namespace in PHPStorm
Navigate to your PhpStorm settings and find the "Directories" section. This is our current project structure. Open object-translation-bundle and click the src directory. Above, mark it as "Sources". Now PhpStorm knows it's a "source" directory, and it was added on the right here.
Click the little pencil icon to set the namespace, or "package prefix" for this source. Paste, remove the extra slashes, and click "OK".
Hit "Apply" and "OK" to save this update, and close the composer.json file.
Creating Your Class
Again, create a new PHP class in our bundle's src/ directory. Awesome! The namespace is now pre-filled! Call it ObjectTranslationBundle.
Here's our bundle class. First, mark it as final - we don't want anyone to extend this. Leave it empty for now and have it extend AbstractBundle from the HttpKernel component:
|
Show Lines
|
// ... lines 1 - 2 |
| namespace SymfonyCasts\ObjectTranslationBundle; | |
| use Symfony\Component\HttpKernel\Bundle\AbstractBundle; | |
| final class ObjectTranslationBundle extends AbstractBundle | |
| { | |
| } |
Enhanced Bundle Class
If you've ever built bundles in the past, specifically before Symfony 6, you might remember bundle classes used to extend Bundle, not AbstractBundle.
In Symfony 6, the way bundles are built was enhanced. The new AbstractBundle class provides a more streamlined approach to bundle development. You can now house almost all your bundle configuration and extension logic directly within your "bundle" class. The old way required several additional classes. Check out this blog post for more details.
Next, let's "install" this bundle in our app!
2 Comments
Hey @seb-jean!
Yep, that's totally valid. Here are a few thoughts/reasons:
- I strayed away because I wanted to show how to customize the alias in a later chapter.
- This is my personal preference. It bothers me to have the namespace in a class name. Reminds me too much of the dark times before PHP has namespaces.
- if your bundle has Twig templates, this makes the template names shorter.
@ObjectTranslation/template.html.twigvs@SymfonyCastsObjectTranslation/template.html.twig
Bottom line: mostly personal preference on my part.
Cheers!
Kevin
"Houston: no signs of life"
Start the conversation!
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": ">=8.2",
"ext-ctype": "*",
"ext-iconv": "*",
"doctrine/dbal": "^3.10.0", // 3.10.0
"doctrine/doctrine-bundle": "^2.15", // 2.15.0
"doctrine/doctrine-migrations-bundle": "^3.4.2", // 3.4.2
"doctrine/orm": "^3.5.0", // 3.5.0
"league/commonmark": "^2.7.1", // 2.7.1
"symfony/asset": "7.3.*", // v7.3.0
"symfony/asset-mapper": "7.3.*", // v7.3.0
"symfony/console": "7.3.*", // v7.3.1
"symfony/crowdin-translation-provider": "7.3.*", // v7.3.0
"symfony/dotenv": "7.3.*", // v7.3.0
"symfony/flex": "^2.8.1", // v2.8.1
"symfony/framework-bundle": "7.3.*", // v7.3.1
"symfony/runtime": "7.3.*", // v7.3.1
"symfony/stimulus-bundle": "^2.28", // v2.28.0
"symfony/translation": "7.3.*", // v7.3.1
"symfony/twig-bundle": "7.3.*", // v7.3.1
"symfony/yaml": "7.3.*", // v7.3.1
"symfonycasts/object-translation-bundle": "@dev", // dev-_tuts/bundle/3-install-our-bundle
"symfonycasts/tailwind-bundle": "^0.10.1", // v0.10.1
"twig/extra-bundle": "^2.12|^3.21", // v3.21.0
"twig/markdown-extra": "^3.21", // v3.21.0
"twig/string-extra": "^3.21", // v3.21.0
"twig/twig": "^2.12|^3.21.1" // v3.21.1
},
"require-dev": {
"phpunit/phpunit": "^9.6.23", // 9.6.23
"symfony/browser-kit": "7.3.*", // v7.3.0
"symfony/css-selector": "7.3.*", // v7.3.0
"symfony/debug-bundle": "7.3.*", // v7.3.0
"symfony/maker-bundle": "^1.64", // v1.64.0
"symfony/monolog-bundle": "^3.10", // v3.10.0
"symfony/phpunit-bridge": "^7.3.1", // v7.3.1
"symfony/stopwatch": "7.3.*", // v7.3.0
"symfony/web-profiler-bundle": "7.3.*", // v7.3.1
"zenstruck/browser": "^1.9.1", // v1.9.1
"zenstruck/foundry": "^2.6.1" // v2.6.1
}
}
Following the best practices at https://symfony.com/doc/current/bundles/best_practices.html#bundle-name, I would have named the bundle
SymfonyCastsObjectTranslationBundleinstead ofObjectTranslationBundle. What do you think?