Laravel 4:将数据从make传递到服务提供商 [英] Laravel 4: Passing data from make to the service provider
本文介绍了Laravel 4:将数据从make传递到服务提供商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面的代码说明了一切...
The code below says it all...
// routes.php
App::make('SimpleGeo',array('test')); <- passing array('test')
// SimpleGeoServiceProvider.php
public function register()
{
$this->app['SimpleGeo'] = $this->app->share(function($app)
{
return new SimpleGeo($what_goes_here);
});
}
// SimpleGeo.php
class SimpleGeo
{
protected $_test;
public function __construct($test) <- need array('test')
{
$this->_test = $test;
}
public function getTest()
{
return $this->_test;
}
}
推荐答案
您可以尝试将带有参数的类直接绑定到您的应用容器中,例如
You can try to bind the class with the parameters directly into your app container, like
<?php // This is your SimpleGeoServiceProvider.php
use Illuminate\Support\ServiceProvider;
Class SimpleGeoServiceProvider extends ServiceProvider {
public function register()
{
$this->app->bind('SimpleGeo', function($app, $parameters)
{
return new SimpleGeo($parameters);
});
}
}
使您的SimpleGeo.php保持不变.您可以在routes.php中对其进行测试
leaving untouched your SimpleGeo.php. You can test it in your routes.php
$test = App::make('SimpleGeo', array('test'));
var_dump ($test);
这篇关于Laravel 4:将数据从make传递到服务提供商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文