goravel
  • README
  • ORM
    • getting-started
    • Migrations
    • Relationships
  • Architecutre Concepts
    • Facades
    • Request Lifecycle
    • Service Container
    • Service Providers
  • Digging Deeper
    • Artisan Console
    • Cache
    • Events
    • File Storage
    • Helpers
    • Mail
    • Mock
    • Package Development
    • Queues
    • Task Scheduling
  • Getting Started
    • Compile
    • Configuration
    • Directory Structure
    • Installation
  • prologue
    • Contribution Guide
    • Excellent Extend Packages
  • security
    • Authentication
    • Authorization
    • Encryption
    • Hashing
  • The Basics
    • Controllers
    • Grpc
    • Logging
    • Middleware
    • HTTP Requests
    • HTTP Response
    • Routing
    • Validation
  • upgrade
    • History Upgrade
    • Upgrading To v1.1 From v1.0
    • Upgrading To v1.10 From v1.9
    • Upgrading To v1.11 From v1.10
    • Upgrading To v1.12 From v1.11
    • Upgrading To v1.2 From v1.1
    • Upgrading To v1.3 From v1.2
    • Upgrading To v1.4 From v1.3
    • Upgrading To v1.5 From v1.4
    • Upgrading To v1.6 From v1.5
    • Upgrading To v1.7 From v1.6
    • Upgrading To v1.8 From v1.7
    • Upgrading To v1.9 From v1.8
  • zh
    • ORM
      • 快速入门
      • 数据库迁移
      • 模型关联
    • 核心架构
      • Facades
      • 请求周期
      • 服务容器
      • 服务提供者
    • 综合话题
      • Artisan 命令行
      • 缓存系统
      • 事件系统
      • 文件存储
      • 辅助函数
      • 发送邮件
      • Mock
      • 扩展包开发
      • 队列
      • 任务调度
    • 入门指南
      • 编译
      • 配置信息
      • 文件夹结构
      • 安装
    • prologue
      • 贡献指南
      • 优秀扩展包
    • security
      • 用户认证
      • 用户授权
      • 加密解密
      • 哈希
    • 基本功能
      • 控制器
      • Grpc
      • 日志
      • HTTP 中间件
      • 请求
      • 响应
      • 路由
      • 表单验证
    • upgrade
      • 历史版本升级
      • 从 v1.0 升级到 v1.1
      • 从 v1.9 升级到 v1.10
      • 从 v1.10 升级到 v1.11
      • 从 v1.11 升级到 v1.12
      • 从 v1.1 升级到 v1.2
      • 从 v1.2 升级到 v1.3
      • 从 v1.3 升级到 v1.4
      • 从 v1.4 升级到 v1.5
      • 从 v1.5 升级到 v1.6
      • 从 v1.6 升级到 v1.7
      • 从 v1.7 升级到 v1.8
      • 从 v1.8 升级到 v1.9
Powered by GitBook
On this page
  • Introduction
  • Binding
  • Simple Bindings
  • Binding A Singleton
  • Binding Instances
  • Binding With Parameter
  • Resolving
  • The Make Method
  • The MakeWith Method
  • Other Methods
Edit on GitHub
  1. Architecutre Concepts

Service Container

PreviousRequest LifecycleNextService Providers

Last updated 1 year ago

[[toc]]

Introduction

The Goravel service container is a powerful tool for managing class dependencies and performing dependency injection. It contains all the modules of Goravel, and allows you to bind your own services to container and resolve them when needed. The service container provides powerful support for third-party packages around Goravel.

Binding

Simple Bindings

Almost all of your service container bindings will be registered within . Within a service provider, you always have access to the container via the app parameter, then register a binding using the Bind method, passing the key that we wish to register along with a closure that returns an instance of the class:

package route

import (
	"github.com/goravel/framework/contracts/foundation"
)

const Binding = "goravel.route"

type ServiceProvider struct {
}

func (route *ServiceProvider) Register(app foundation.Application) {
	app.Bind(Binding, func() (any, error) {
		return NewRoute(app.MakeConfig()), nil
	})
}

func (route *ServiceProvider) Boot(app foundation.Application) {

}

As mentioned, you will typically be interacting with the container within service providers; however, if you would like to interact with the container outside of a service provider, you may do so via the App facade:

facades.App().Bind("key", func() (any, error) {
    ...
})

Binding A Singleton

The Singleton method binds a class or interface into the container that should only be resolved one time. Once a singleton binding is resolved, the same object instance will be returned on subsequent calls into the container:

app.Singleton(key, func() (any, error) {
    return NewGin(app.MakeConfig()), nil
})

Binding Instances

You may also bind an existing object instance into the container using the Instance method. The given instance will always be returned on subsequent calls into the container:

app.Instance(key, instance)

Binding With Parameter

If you need some extra parameters to construct the service provider, you can use the BindWith method to pass parameters to the closure:

app.BindWith(Binding, func(parameters map[string]any) (any, error) {
    return NewRoute(app.MakeConfig()), nil
})

Resolving

The Make Method

You may use the Make method to resolve a class instance from the container. The Make method accepts the key you wish to resolve:

instance, err := app.Make(key)

If you are outside of a service provider in a location of your code that does not have access to the app variable, you may use the App facade to resolve a class instance from the container:

instance, err := facades.App().Make(key)

The MakeWith Method

If some of your class's dependencies are not resolvable via the container, you may inject them by passing them as an associative array into the MakeWith method, corresponding to the BindWith binding method:

instance, err := app.MakeWith(key, map[string]any{"id": 1})

Other Methods

The framework providers some convenient methods to quickly resolve various facade: MakeArtisan, MakeAuth, MakeCache, MakeConfig, MakeCrypt, MakeEvent, MakeGate, MakeGrpc, MakeHash, MakeLog, MakeMail, MakeOrm, MakeQueue, MakeRateLimiter, MakeRoute, MakeSchedule, MakeStorage, MakeValidation.

service providers