Rav*_*esh 3 laravel laravel-5.7 laravel-service-container
需要通过一个例子来了解 Laravel 服务容器和服务提供者。
H45*_*45H 11
服务容器是您的服务注册的地方。
服务提供者通过将服务添加到容器中来提供服务。
参考Laracast。注意理解。
服务容器:https://laracasts.com/series/laravel-from-scratch-2017/episodes/24
服务提供商:https://laracasts.com/series/laravel-from-scratch-2017/episodes/25
您好,欢迎来到 stackoverflow!
服务容器是bindings存储我们的应用程序的地方。而服务供应商是我们注册了绑定到类服务容器。在旧版本的 Laravel 中,我们没有这些提供者,人们总是问在哪里放置绑定。答案令人困惑。“最有意义的地方。”!然后,Laravel 引入了这些服务提供者和提供者目录,以便为人们清理。
为了便于理解,我将包含一个基本示例:
interface AcmeInterface {
public function sayHi();
}
class AcmeImplementation implements AcmeInterface {
public function sayHi() {
echo 'Hi!';
}
}
// Service Container
$app = new \Illuminate\Database\Container;
// Some required stuff that are also service providing lines
// for app config and app itself.
$app->singleton('app', 'Illuminate\Container\Container');
$app->singleton('config', 'Illuminate\Config\Repository');
// Our Example Service Provider
$app->bind(AcmeInterface::class, AcmeImplementation::class);
// Example Usage:
$implementation = $app->make(AcmeInterface::class);
$implementation->sayHi();
Run Code Online (Sandbox Code Playgroud)
正如你看到的;
bootstrap/app.php),config/app.php),| 归档时间: |
|
| 查看次数: |
5488 次 |
| 最近记录: |