您好,欢迎访问一九零五行业门户网

求大神 解释下 Laravel 文档中的 Service Providers

官方文档
app->singleton('riak\contracts\connection', function ($app) { return new connection(config('riak')); }); }}

riak\contracts\connection这个是要自己实现吗 ?
回复内容: 官方文档
app->singleton('riak\contracts\connection', function ($app) { return new connection(config('riak')); }); }}

riak\contracts\connection这个是要自己实现吗 ?
这个是另一个package,你说的“自己实现”,其实也对的,这个地方也可以放你自己定义的class
其实这段代码产生的效果是
比如
$this->app->singleton('reportservices', function () { return new \app\services\reportservices();});
以后在其他class里我可以用
app('reportservices')->xxxxxx();
去代替
$services = new \app\services\reportservices();$services->xxxxxx();
providers的存在目的就是在程序启动的时候注册各种东西,比如你要扩展cache类去使用阿里云ocs
public function boot() { //扩展阿里云ocs缓存 cache::extend('ocs', function ($app, $config) { $prefix = $app['config']['cache.prefix']; $memcached = new \memcached; foreach ($config['servers'] as $server) { $memcached->addserver( $server['host'], $server['port'], $server['weight'] ); if (ini_get('memcached.use_sasl')) { $user = $server['authname']; $pass = $server['authpass']; $memcached->setoption(\memcached::opt_compression, false); $memcached->setoption(\memcached::opt_binary_protocol, true); $memcached->setsaslauthdata($user, $pass); } } $store = new \illuminate\cache\memcachedstore($memcached, $prefix); return new \illuminate\cache\repository($store); });}
比如你要扩展表单验证类
public function boot(){ //扩展表单验证 validator::extend('greater_than', function ($attribute, $value, $parameters) { $other = request::input($parameters[0]); return isset($other) && intval($value) > intval($other); });}
其它类似信息

推荐信息