The laravel-4 method for injecting an object that requires tuning to the controller

I would like to find a good way to pass a pre-configured object to a controller. I know that I can use IoC as shown below:

Mycontroller extends extends \Illuminate\Routing\Controllers\Controller { //i can only use one config uless i pass Request data $this->config = App::make('MyconfigObject'); } 

but this seems to limit the ability to use only one configuration. I would rather do something like the following:

 Route::get('some-route', function() { $config = Config::get('some.config'); $object = new MyConfigObject($config); Route::dispatch(MyController($object)); }); 

The reason I would like to do this is because I would like to send the same controller, but with a different configuration for multiple routes.

+2
source share
1 answer

I am not completely satisfied with this method, but its the best I have come up with so far using IoC auto-resolution.

bootstrap / stat.php

 /* * bindings to the IoC container */ $app->singleton('MyNamespace\Transfer\TransferStategyInterface', function() { $config = Config::get('transfer-strategy'); return new LocalTransferStrategy($config); }); use MyNamespace\Transfer\TransferStategyInterface; 

TransferController.php

 use MyNamespace\Transfer\TransferStategyInterface; class TransferController extends BaseController { protected $transferStrategy; public function __construct(TransferStategyInterface $transferStrategy = null) { $this->transferStrategy = $transferStrategy; } } 
+1
source

All Articles