Laravel 5.7 Service Container | DI / Passing parameters to a method in a controller
I'm experiencing difficulties to find a elegant solution to inject parameters into controller methods. I want to move my factories into the service container because the code more complicated for inexperienced programmers. I tried to inject parameters into a controller function using the bindMethod of the service container but i didn't succeed yet.
Does someone know how to accomplish this?
Code I tried to inject parameters into controller methods
Laravel AppServiceProvider
<?php
namespace AppProviders;
use IlluminateSupportServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$container = app();
$container->bindMethod('TestController@index', function ($controller, $container) {
return $controller->index('TestData');
});
}
}
Controller
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
class TestController extends Controller
{
public function index($test)
{
// do something with the implementations.
}
}
Example of what I want to achieve
The current situation (simplified).
public function index()
{
$car = CarFactory::get();
$food = FoodFactory::get();
}
I want it to look like this:
public function index($car, $food)
{
// do something with the implementations.
}
thanks in advance!
php laravel
add a comment |
I'm experiencing difficulties to find a elegant solution to inject parameters into controller methods. I want to move my factories into the service container because the code more complicated for inexperienced programmers. I tried to inject parameters into a controller function using the bindMethod of the service container but i didn't succeed yet.
Does someone know how to accomplish this?
Code I tried to inject parameters into controller methods
Laravel AppServiceProvider
<?php
namespace AppProviders;
use IlluminateSupportServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$container = app();
$container->bindMethod('TestController@index', function ($controller, $container) {
return $controller->index('TestData');
});
}
}
Controller
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
class TestController extends Controller
{
public function index($test)
{
// do something with the implementations.
}
}
Example of what I want to achieve
The current situation (simplified).
public function index()
{
$car = CarFactory::get();
$food = FoodFactory::get();
}
I want it to look like this:
public function index($car, $food)
{
// do something with the implementations.
}
thanks in advance!
php laravel
1
Show us how you are currently trying tobind
in the container
– jszobody
Jan 3 at 19:26
@jszobody adding it right now, give me a sec.
– Romano Schoonheim
Jan 3 at 19:29
There's no need to unnecessarily bind stuff to a container. You could Type Hint your dependencies and Laravel is going to inject it for you as a dependency using Reflection.
– Mozammil
Jan 3 at 19:38
add a comment |
I'm experiencing difficulties to find a elegant solution to inject parameters into controller methods. I want to move my factories into the service container because the code more complicated for inexperienced programmers. I tried to inject parameters into a controller function using the bindMethod of the service container but i didn't succeed yet.
Does someone know how to accomplish this?
Code I tried to inject parameters into controller methods
Laravel AppServiceProvider
<?php
namespace AppProviders;
use IlluminateSupportServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$container = app();
$container->bindMethod('TestController@index', function ($controller, $container) {
return $controller->index('TestData');
});
}
}
Controller
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
class TestController extends Controller
{
public function index($test)
{
// do something with the implementations.
}
}
Example of what I want to achieve
The current situation (simplified).
public function index()
{
$car = CarFactory::get();
$food = FoodFactory::get();
}
I want it to look like this:
public function index($car, $food)
{
// do something with the implementations.
}
thanks in advance!
php laravel
I'm experiencing difficulties to find a elegant solution to inject parameters into controller methods. I want to move my factories into the service container because the code more complicated for inexperienced programmers. I tried to inject parameters into a controller function using the bindMethod of the service container but i didn't succeed yet.
Does someone know how to accomplish this?
Code I tried to inject parameters into controller methods
Laravel AppServiceProvider
<?php
namespace AppProviders;
use IlluminateSupportServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$container = app();
$container->bindMethod('TestController@index', function ($controller, $container) {
return $controller->index('TestData');
});
}
}
Controller
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
class TestController extends Controller
{
public function index($test)
{
// do something with the implementations.
}
}
Example of what I want to achieve
The current situation (simplified).
public function index()
{
$car = CarFactory::get();
$food = FoodFactory::get();
}
I want it to look like this:
public function index($car, $food)
{
// do something with the implementations.
}
thanks in advance!
php laravel
php laravel
edited Jan 3 at 19:33
Romano Schoonheim
asked Jan 3 at 19:25
Romano SchoonheimRomano Schoonheim
6810
6810
1
Show us how you are currently trying tobind
in the container
– jszobody
Jan 3 at 19:26
@jszobody adding it right now, give me a sec.
– Romano Schoonheim
Jan 3 at 19:29
There's no need to unnecessarily bind stuff to a container. You could Type Hint your dependencies and Laravel is going to inject it for you as a dependency using Reflection.
– Mozammil
Jan 3 at 19:38
add a comment |
1
Show us how you are currently trying tobind
in the container
– jszobody
Jan 3 at 19:26
@jszobody adding it right now, give me a sec.
– Romano Schoonheim
Jan 3 at 19:29
There's no need to unnecessarily bind stuff to a container. You could Type Hint your dependencies and Laravel is going to inject it for you as a dependency using Reflection.
– Mozammil
Jan 3 at 19:38
1
1
Show us how you are currently trying to
bind
in the container– jszobody
Jan 3 at 19:26
Show us how you are currently trying to
bind
in the container– jszobody
Jan 3 at 19:26
@jszobody adding it right now, give me a sec.
– Romano Schoonheim
Jan 3 at 19:29
@jszobody adding it right now, give me a sec.
– Romano Schoonheim
Jan 3 at 19:29
There's no need to unnecessarily bind stuff to a container. You could Type Hint your dependencies and Laravel is going to inject it for you as a dependency using Reflection.
– Mozammil
Jan 3 at 19:38
There's no need to unnecessarily bind stuff to a container. You could Type Hint your dependencies and Laravel is going to inject it for you as a dependency using Reflection.
– Mozammil
Jan 3 at 19:38
add a comment |
2 Answers
2
active
oldest
votes
It is possible to use a factory to inject a implementation of a specific interface with the service container. You could do that with the bind method.
<?php
namespace AppProviders;
use AppExampleImplementation;
use IlluminateSupportServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(
'AppIExample',
function() {
return (new factory())->make();
}
);
}
}
I'm researching of this is a practical approach to remove factories from controllers.
Thanks guys!
If this is the correct answer, please select it so others can see it.
– Camilo
Jan 3 at 20:35
I will! I have to wait 2 days before I can accept my own answer.
– Romano Schoonheim
Jan 3 at 20:36
add a comment |
You can do it in the constructor:
private $carFactory;
public function __construct(CarFactory $carFactory){
$this->carFactory = $carFactory;
}
Then access it anytime with $this->carFactory
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54028594%2flaravel-5-7-service-container-di-passing-parameters-to-a-method-in-a-control%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
It is possible to use a factory to inject a implementation of a specific interface with the service container. You could do that with the bind method.
<?php
namespace AppProviders;
use AppExampleImplementation;
use IlluminateSupportServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(
'AppIExample',
function() {
return (new factory())->make();
}
);
}
}
I'm researching of this is a practical approach to remove factories from controllers.
Thanks guys!
If this is the correct answer, please select it so others can see it.
– Camilo
Jan 3 at 20:35
I will! I have to wait 2 days before I can accept my own answer.
– Romano Schoonheim
Jan 3 at 20:36
add a comment |
It is possible to use a factory to inject a implementation of a specific interface with the service container. You could do that with the bind method.
<?php
namespace AppProviders;
use AppExampleImplementation;
use IlluminateSupportServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(
'AppIExample',
function() {
return (new factory())->make();
}
);
}
}
I'm researching of this is a practical approach to remove factories from controllers.
Thanks guys!
If this is the correct answer, please select it so others can see it.
– Camilo
Jan 3 at 20:35
I will! I have to wait 2 days before I can accept my own answer.
– Romano Schoonheim
Jan 3 at 20:36
add a comment |
It is possible to use a factory to inject a implementation of a specific interface with the service container. You could do that with the bind method.
<?php
namespace AppProviders;
use AppExampleImplementation;
use IlluminateSupportServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(
'AppIExample',
function() {
return (new factory())->make();
}
);
}
}
I'm researching of this is a practical approach to remove factories from controllers.
Thanks guys!
It is possible to use a factory to inject a implementation of a specific interface with the service container. You could do that with the bind method.
<?php
namespace AppProviders;
use AppExampleImplementation;
use IlluminateSupportServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(
'AppIExample',
function() {
return (new factory())->make();
}
);
}
}
I'm researching of this is a practical approach to remove factories from controllers.
Thanks guys!
edited Jan 3 at 20:41
answered Jan 3 at 20:30
Romano SchoonheimRomano Schoonheim
6810
6810
If this is the correct answer, please select it so others can see it.
– Camilo
Jan 3 at 20:35
I will! I have to wait 2 days before I can accept my own answer.
– Romano Schoonheim
Jan 3 at 20:36
add a comment |
If this is the correct answer, please select it so others can see it.
– Camilo
Jan 3 at 20:35
I will! I have to wait 2 days before I can accept my own answer.
– Romano Schoonheim
Jan 3 at 20:36
If this is the correct answer, please select it so others can see it.
– Camilo
Jan 3 at 20:35
If this is the correct answer, please select it so others can see it.
– Camilo
Jan 3 at 20:35
I will! I have to wait 2 days before I can accept my own answer.
– Romano Schoonheim
Jan 3 at 20:36
I will! I have to wait 2 days before I can accept my own answer.
– Romano Schoonheim
Jan 3 at 20:36
add a comment |
You can do it in the constructor:
private $carFactory;
public function __construct(CarFactory $carFactory){
$this->carFactory = $carFactory;
}
Then access it anytime with $this->carFactory
add a comment |
You can do it in the constructor:
private $carFactory;
public function __construct(CarFactory $carFactory){
$this->carFactory = $carFactory;
}
Then access it anytime with $this->carFactory
add a comment |
You can do it in the constructor:
private $carFactory;
public function __construct(CarFactory $carFactory){
$this->carFactory = $carFactory;
}
Then access it anytime with $this->carFactory
You can do it in the constructor:
private $carFactory;
public function __construct(CarFactory $carFactory){
$this->carFactory = $carFactory;
}
Then access it anytime with $this->carFactory
answered Jan 3 at 19:56
JeffJeff
15.7k13559
15.7k13559
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54028594%2flaravel-5-7-service-container-di-passing-parameters-to-a-method-in-a-control%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Show us how you are currently trying to
bind
in the container– jszobody
Jan 3 at 19:26
@jszobody adding it right now, give me a sec.
– Romano Schoonheim
Jan 3 at 19:29
There's no need to unnecessarily bind stuff to a container. You could Type Hint your dependencies and Laravel is going to inject it for you as a dependency using Reflection.
– Mozammil
Jan 3 at 19:38