last day (16 days later) » 

1:36 PM
0
A: How to use binded implementation in Laravel Job Class?

JackYou can inject dependencies in the handle method. class SendinBlueJob implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; public $message; public function __construct(MessageInterface $message) { $this->mes...

 
KKK
It's not possible, bcz I have to exec handle method from constructor but in constructor I don't have $messageProvider var, so I cant pass it to method.
 
You shouldn't be calling handle in your constructor as the method isn't necessarily called when the class is instantiated, for example when you push a job to a queue, the handle method is called when the queue worker processes the job. Try the code above and it should work as you expect.
 
KKK
Nah, without calling handle method job is never called to execute if it's not synchronous but Redis queue p.ex.
 
Because you have no queue worker setup... you're making it synchronous.
 
KKK
I ran queue:work, but it crashes with same error: Target [App\Interfaces\MessageProviderInterface] is not instantiable.
 
1:37 PM
Are you still passing app()->make(MessageProviderInterface::class) in your observer?
 
KKK
No.
Hello
Thanks for help.
I'm not passing it, bcz it's removed from constructor and moved to handle method which I don't exec from observer.
 
ah i see
Apologies, contextual binding doesn't actually work via a method (github.com/laravel/framework/issues/6177)
 
KKK
Yeah, I read this before.
I am working with this issue like 1,5 day.
 
This means you can't contextual bind in a job because you cant dependency inject through the constructor.
the $parameters passed to the construct are explicit.
All I can suggest is to inject the concrete implementation.
Either that or create a new interface which extends your base interface and then app()->bind(SendInBlueMessageProviderInterface::class, SendInBlueService::class)
Contextual binding is a bit of a half baked feature in laravel and I wouldn't really recommend using it outside of Controllers.
 
KKK
Hmn, ok.
It looks awesome in docs.
 
1:53 PM
Just thought another option would be to dispatch your job from your controller, then you could load the message provider in your controller and pass it in as the second parameter to your job.
 
KKK
I am not using controller, only observer.
 
I'll update my answer if that sounds like it could work for you? Would mean it could no longer be abstracted into an observer.
 
KKK
I try to move logic outside controller.
 
would that be possible in your application?
i.e is there a reason you're using an observer or is it just a means of refactoring?
 
KKK
I like this pattern.
It looks very clear for me.
 
1:56 PM
Okay, well the way I suggested is the only way I can see contextual binding working for your use case.
 
KKK
Yeah.
 
I'll update my answer anyway and you can see if you like the implementation :) Good luck.
 
KKK
Thanks.
 
 
2 hours later…
KKK
3:48 PM
The one thing, which is interesting me, how you deducted that in Job class I can use only method binding?
There is no info in docs, that contextual binds work only in Controller classes.
 

  last day (16 days later) »