时间:2021-07-01 10:21:17 帮助过:3人阅读
app->singleton('Riak\Contracts\Connection', function ($app) {
return new Connection(config('riak'));
});
}
}
这个是要自己实现吗 ?
官方文档
app->singleton('Riak\Contracts\Connection', function ($app) {
return new Connection(config('riak'));
});
}
}
这个是要自己实现吗 ?
这个是另一个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);
});
}