Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
laravel-4将需要配置的对象注入控制器的方法_Laravel_Laravel 4 - Fatal编程技术网

laravel-4将需要配置的对象注入控制器的方法

laravel-4将需要配置的对象注入控制器的方法,laravel,laravel-4,Laravel,Laravel 4,我想找到一种好方法,将预配置的对象传递给控制器。我知道我可以像下面这样使用国际奥委会: Mycontroller extends extends \Illuminate\Routing\Controllers\Controller { //i can only use one config uless i pass Request data $this->config = App::make('MyconfigObject'); } 但这似乎有局限性,只能使用一个配置

我想找到一种好方法,将预配置的对象传递给控制器。我知道我可以像下面这样使用国际奥委会:

Mycontroller extends extends \Illuminate\Routing\Controllers\Controller {

    //i can only use one config uless i pass Request data
    $this->config = App::make('MyconfigObject');

}
但这似乎有局限性,只能使用一个配置。我宁愿做以下事情:

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

我之所以想这样做,是因为我想调度同一个控制器,但多条路线的配置不同。

我对这种方法并不完全满意,但它是迄今为止我想到的最好的方法,使用IoC的自动分辨率

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;
    }
}

我以前用过你的方法。现在我使用
App::make()
直接在方法中实例化对象。这样,您就不需要创建某些方法中不需要的对象,并保持控制器的简单。其他提示:我在应用程序绑定中不使用反激光。我让它自动解决。我使用点,比如
App::make('my.special.method',array($arg1,$arg2))
。通过这种方式,我可以拥有同一个类的多个构造函数,并具有描述性实例化