10.
services.yaml & the Amazing bind
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.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
27 Comments
Its logging on markdown.log and dev.log.
Any solution?
Got the same issue. The
markdown_logginghandler is only logging themarkdownchannel but themainhandler is logging anything except theeventchannel. You have to exclude themarkdownchannel from themainhandler within themonolog.yaml.Hey NickReynke!
Ah, great work! This was on oversight on my part! And your solution is perfect.
Cheers!
Thanks for reply. I also found this solution but it looks like its a bad design. Because when I am specifying a channel to log, why it should also publish it on main log file by default.
The `markdown` channel, when created within a new handler (`markdown_logging` in this case), is also logged by the `main` handler because the `main` handler is by default only excluding the `event` channel, but not the new `markdown` channel.
It makes sense now. Thanks for describing.
Awesome ! I really begin understanding what happens behind the scenes. Thank you Ryan !
I just read the monolog docs here: https://symfony.com/doc/4.4...
And it seems bind is obsolete in this case.
Just the parameter name hinting is sufficient since 3.5. No change to services.yaml required.
I removed the bind, cleared the cache and ... it works.
Did I miss something?
Hey Richard
Cool! So the binding it's now done by the bundle automatically, you just need to remember the convention. That feature didn't exist when this tutorials was written :)
Cheers!
I added Service to the excludes to test in services.yaml:
exclude: '../src/{Service,Entity,Migrations,Tests}'
And it still found MarkdownHelp in the Service directory. Bug? (I cleared the cache too).
Hey Richard
I believe you still have the custom configuration for the MarkdownHelp class. Could you double check it?
Cheers!
I believe you may have been right.. but worse than that... I had "Services" and not "Service" in the exclude. So ignore the noise...
NP!
Hello!
I am trying to use lazy loading, symfony 4.3,
configuring in services.yaml
When trying to call:
I get error:
Any idea?
Thanks
Hey Avraham,
Are you sure you need to lazy loading *everything* in your application? It's not a good practice, actually, I think it's just overkill. Well, except service you also have entities, models, forms, etc - they probably don't need to be lazy loaded. I'd recommend you to start configuring lazy loading for 1 service where you think lazy loading is the most important, get it working, then switch to another class that also should be lazy loaded, and so one. Try to configure it class by class - it would be easier to debug things I think.
About the error, first of all, try to clear the cache the the specific environment where you see the error, and try again. Do you see the same error? Do you have a staticProxyConstructor() method call somewhere in *your* code? If no, probably some version incompatibility, could you try to update Composer dependencies, clear the cache, and try again?
Cheers!
Hey Viktor,
Thanks for your advice.
Yes, I turn off lazy option.I succeed to obtain $entityManager = $this->getDoctrine()->getManager();
Yet still MappingException: Class "App\Entity\Patient" is not a valid entity or mapped super class.
<br />$patient = new Patient();<br />$patient->setFname('Alex1');<br />// FAILS<br />// MappingException: Class "App\Entity\Patient" is not a valid entity or mapped super class.<br />$entityManager->persist($patient); <br />Great, let's follow the second part of your comment in another thread to avoid duplications: https://symfonycasts.com/sc...
Cheers!
Sure,
Thanks!
I have a question about 'Services are only Instantiated Once'.
What if we need to process some data in our service (for example some Converter) and we don't want to pass all the data (some arrays for example) to each method so we make this class keep the state in properties (but also uses some services from container)?
I saw that there is an option:
shared: falseto Define Non Shared Services so it will create a new instance every time and I also found on the stackoverflow similar question: https://stackoverflow.com/questions/28511553/symfony2-services-with-or-without-state but there were just two answers.So how should we do something like that? Use Non Shared Services; create stateless services, inject dependencies and pass them to statefull classes created by 'new' keyword inside service or some other way?
Hey Agata,
Sorry for the long reply! Yes, using "shared: false" is a valid way to achieve what you need, and it sounds OK to me. I don't know your design to much, but if you think there's no other way and you do need to use different objects in different places instead of one shared - go for it.
Cheers!
Hey,
When i try to test if it wil log but i get this weird error
<br />invalidArgumentException<br />Invalid service "App\Service\MarkdownHelper": method "__construct()" has no argument named "$logger". Check your service definition.even though i follow exactly like the tutorials and i can't seem to figure out where the problem lies.
Any solution why?
UPDATE
Found the solution i still had some code in the services.yaml after i quoted that out it works :)
`
`
Forgot this bit now it's disabled.
Hey Jeffrey C.
Yep, indentation matters in YAML files :). Cheers!
Hi.
I had used bind configuration con my project. I am using Symfony 4.1 and when I add more than one I get this error:
```
Unused binding "$variableName" in service ".abstract.instanceof.App\Twig\AppExtension".
```
And that is right, I don't inject this variable into that twig extension because I do not use it there
I found this question but without a correct answer: https://openclassrooms.com/...
Did you now something about this?
Side note: if I remove the bind variable or inject this variable into my AppExtension constructor it works. But this not make any sense
Hey micayael!
Ah yes, I can answer this! My guess is that, you have some configuration that looks like this:
When you use bind, to help make sure you don't have a typo, you MUST have a
$variableNameargument (with that name) in the constructor of one of your services. If you have ZERO arguments with this name, then, because this might be a typo, Symfony throws an exception. However, there is a small bug in the exception message. Really, the message should say:The bug is that, internally, Symfony "attaches" the error to one specific service - in your case some internal ".abstract.instanceof.App\Twig\AppExtension".
So, the fix is to either (A) make sure that each bind is used on at least one service that it's bound to or (B) remove the bind because it's unused.
Let me know if that helps!
Cheers!
I have this configuration
~~~
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
public: false # Allows optimizing the container by removing unused services; this also means
# fetching services directly from the container via $container->get() won't work.
# The best practice is to be explicit about your dependencies anyway.
bind:
$variableName1: '%param1%'
$variableName2: '%param2%'
$variableName3: '@csa_guzzle.client.api'
~~~
The 3 are in at least one constructor. The one that gives me problems is injected into a command (is a parameter). Could this be a problem?
I got it
There was the old configuration that I was using before trying with your tutorial
App\Command\GreatCommand:
arguments:
$variableName2: '%param2%'
tags:
- { name: 'console.command' }
I deleted it and it worked. Thanks
"Houston: no signs of life"
Start the conversation!