Chapters
23 Chapters
|
2:20:28
|
Login to bookmark this video
-
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!
09.
Creating a Service
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
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.
This tutorial also works great for Symfony 6!
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.3.0 || ^8.0.0",
"ext-ctype": "*",
"ext-iconv": "*",
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"knplabs/knp-markdown-bundle": "^1.8", // 1.9.0
"sensio/framework-extra-bundle": "^6.0", // v6.2.1
"sentry/sentry-symfony": "^4.0", // 4.0.3
"symfony/asset": "5.0.*", // v5.0.11
"symfony/console": "5.0.*", // v5.0.11
"symfony/debug-bundle": "5.0.*", // v5.0.11
"symfony/dotenv": "5.0.*", // v5.0.11
"symfony/flex": "^1.3.1", // v1.21.6
"symfony/framework-bundle": "5.0.*", // v5.0.11
"symfony/monolog-bundle": "^3.0", // v3.6.0
"symfony/profiler-pack": "*", // v1.0.5
"symfony/routing": "5.1.*", // v5.1.11
"symfony/twig-pack": "^1.0", // v1.0.1
"symfony/var-dumper": "5.0.*", // v5.0.11
"symfony/webpack-encore-bundle": "^1.7", // v1.8.0
"symfony/yaml": "5.0.*" // v5.0.11
},
"require-dev": {
"symfony/maker-bundle": "^1.15", // v1.23.0
"symfony/profiler-pack": "^1.0" // v1.0.5
}
}
8 Comments
If I want to create a service class under /src but I want it to be in Some\Other\Namespace rather than App\, how do we do that (in addition to editing the autoload/psr-4 setting in composer.json)?
Hey David,
According to the PSR4, you will need to create the same folder structure in the src/ dir to match the new namespace, i.e. if you want Some\Other\Namespace - then you will need the next file structure: src/Some/Other/Namespace/ and put there your PHP class. Abut the autoload - yeah, you might need to tweak its config in composer.json, though it depends on your current config. Most probably, you can guess it yourself, just duplicate the current config line for App namespace and do the same for yours. I suppose this one will work for you:
This should work. Or, take a look at Composer docs where this all is explained in full: https://getcomposer.org/doc/01-basic-usage.md#autoloading
I hope this helps!
Cheers!
It doesn't seem to be as simple as editing composer.json to get symfony to allow different namespaces to coexist under src. In my composer.json I added the psr-4 namespace:
and I put a class DMz\Weather in a file src/DMz/Weather.php. The class' namespace declaration is
<br />namespace DMz;<br />but I get a LoaderLoadException:
<br />Expected to find class "App\DMz\Weather" in file"/opt/www/new.davidmintz.org/src/DMz/Weather.php" while importing services from resource "../src/", but it was not found! Check the namespace prefix used with the resource in /opt/www/new.davidmintz.org/config/services.yaml (which is being imported from "/opt/www/new.davidmintz.org/src/Kernel.php").and my services.yaml has this appended under the
serviceskey:and I've also tried putting this before -- rather than after -- the App\ entry, with this result:
Hey David,
It does go beyond of this tutorial, but I can give you some minor tips I think ;) Actually, you did everything correct I suppose, probably the only missing piece here is that you have to recompile Composer's autoloader. You can do it by running "composer update --lock" I think :) Or with more specific command: "composer dump-autoload", but don't forget to clear the cache after, but I'd suggest to use "composer update --lock" for simplicity.
I hope this helps!
Cheers!
Oops, I forgot to mention that I did
composer dump-autoloadand I <i>think</i> I also cleared my cache but I will go back and fiddle a little more. Final word: that this is so -- not-easy? -- leads me to think that maybe the philosophy here is: if you think you need a separate namespace, maybe it's because you want to re-use that differently-namespaced-code across multiple projects, and if so, put it in a repository andrequireit with composer, so it ends up living invendor. Could I be right?Thanks again for indulging me!
Hey David,
Yep, mostly like that :) If you need to reuse some code - the best approach would be to create a bundle and then require it via Composer as a dependency, yeah. This way you will store that code in one centrilized place, and if you would ever need to change it - you will be able to change it in one spot and then upgrade the bundle that will change it across your projects - so, there're clearly more benefits with this approach than just copy/paste those files from project to project.
About this namespace thing - well, I think it's still should be easy to do, the biggest problem with the cache probably, i.e. when you did everything correct but still not see any changes because of the cache - this may just mislead. But this behaviour totally work too, it depends on your specific use case and what better for you. But yeah, with the weather example, especially if you want to reuse it in a few project - I'd suggest you to create a separate repo on GitHub and require it in your project.
Cheers!
OMG. I am so sorry for consuming so much of your time. Clearing the cache seems to have been the solution. Someday I will find a way to contribute something to Symfony to redeem myself! (I might be sufficiently qualified to fix typographical and minor usage errors in the English documentation!) Thanks again.
Hey David!
No problem! ;) I'm happy to hear it worked for you eventually - this means the time wan't wasted at all :)
> Someday I will find a way to contribute something to Symfony to redeem myself
This would be awesome! ❤️ Yeah, you can start with Symfony docs! It's much easier than contribute in Symfony core code, and Symfony docs really need help from devs. So, my advice - take a look at Symfony docs, you can add some good example, or add a missing information, or just fix a typo there - everything would be great :)
Cheers!
"Houston: no signs of life"
Start the conversation!