-
Notifications
You must be signed in to change notification settings - Fork 289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How could we use DI with this package #393
Comments
Well, if you have a class with dependencies, and you want to call an instance method (ie. not call it statically), you have to instantiate the object, and therefore have to inject your dependencies. How you do that depends on your application, and isn't directly connected to your the routing framework. You could use a standalone DI library, like: https://github.com/auraphp/Aura.Di Or a lot of the bigger frameworks, like Symfony and Laravel have their own solutions you might be able to re-appropriate. Or you could make your own solution from scratch. However you do it, there is probably a function to call to retrieve your dependency-injected object, like:
Whatever DI magic you use to get your object, you can call that in the route closure:
If your DI system lives in a locally scoped object try the
If have a lot of controllers that all load from your DI system, you might abstract this out to a method, something like:
The key there is that the function returns a function (a closure, but that's really a function), which can be passed to the route method. Using closures/functions/whatever gives you an absurd amount of flexibility, where you can do all kinds of crazy stuff. Some of it you probably should do, but I think this is relatively safe. |
Hi guys
I have a problem with calling callbacks in this package, if I write controller full qualified name in the
respond
method, my controller's action will be called statically, and if I want to instantiate my controller inrespond
method, I have to inject all dependencies of my controller when I want to instantiate it.The text was updated successfully, but these errors were encountered: