-
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
Injecting $app, $req, $res, $services params to controllers? #313
Comments
Don't inject it on the class but pass it along to each action method like klein does:
|
That can be improved I think. Passing 4 parameters to every single method sounds pretty bad imo. |
The parameters are optional if you are not going to use them then don't put
|
I just want to pass them into each controller without having to all 4 variables into methods as seperate parameters. I don't enjoy passing them into methods as parameters everywhere because the app barely be maintainanle like that. That's why I asked this question in first place. Whenever I create a controller, I want them to be injected into the class (without having to specify that myself, Klein should resolve the call itself and append the parameters automatically) so I can organize my code much easier. Thanks for your contributions though, but that's not I'm asking for. |
klein includes a simple dependency injector (see
Then, when you need it, you just ass
and it creates the object with the correct params injected. If you want "smarter" injection, you may want something like php-di, which can look at your classes and try to guess what to inject. |
Request, Response, and Service can be pulled from within the $klein object so I just pass that to my controller or add it to a global singleton registry object. $klein->respond('/search/[results:action]/?[i:page]?', function() use ($klein) {
$klein->service()->layout('app/layouts/2-column.php');
$control = new SearchController( $klein );
$control->run();
}); |
Hey,
I have a code like this:
I want to pass them into a class (not controllers) but doing this each time looks crazy.
Is there any trick or patterns to pass those values to those classes on the fly. For example, can something like this be done?
I just want to inject
$req, $res, $service, $app
to my classes somehow.Any tip would be appreciated.
The text was updated successfully, but these errors were encountered: