扫码关注官方订阅号
官方文档
app->singleton('Riak\Contracts\Connection', function ($app) { return new Connection(config('riak')); }); } }
这个是要自己实现吗 ?
小伙看你根骨奇佳,潜力无限,来学PHP伐。
这个是另一个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); }); }
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
这个是另一个package,你说的“自己实现”,其实也对的,这个地方也可以放你自己定义的class
其实这段代码产生的效果是
比如
以后在其他class里我可以用
去代替
Providers的存在目的就是在程序启动的时候注册各种东西,比如你要扩展Cache类去使用阿里云ocs
比如你要扩展表单验证类