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
  • Creating A Package
  • Service Providers
  • Resources
  • Configuration
  • Routes
  • Migrations
  • Commands
  • Public Assets
  • Publishing File Groups
  • Publish Resources
Edit on GitHub
  1. Digging Deeper

Package Development

PreviousMockNextQueues

Last updated 1 year ago

[[toc]]

Introduction

Packages are the primary way of adding functionality to Goravel, these packages may have routes, controllers, and configuration specifically intended to enhance a Goravel application. This guide primarily covers the development of those packages that are Goravel specific. There is an example for building a third package:

Creating A Package

You can use easily create a package template using the Artisan command:

go run . artisan make:package sms

The created files are saved by default in the root packages folder, you can use --root option to customize:

go run . artisan make:package sms --root=pkg

Service Providers

are the connection point between your package and Goravel, it usually located in the root of the package: service_provider.go. A service provider is responsible for binding things into Goravel's service container and informing Goravel where to load package resources.

Resources

Configuration

Typically, you will need to publish your package's configuration file to the application's config directory. This will allow users of your package to easily override your default configuration options. To allow your configuration files to be published, call the Publishes method from the Boot method of your service provider, the first parameter of this method is the package name, and the second parameter is the mapping between the current package file path and the project path:

func (receiver *ServiceProvider) Boot(app foundation.Application) {
  app.Publishes("github.com/goravel/example-package", map[string]string{
    "config/sms.go": app.ConfigPath("sms.go"),
  })
}

Routes

func (receiver *ServiceProvider) Boot(app foundation.Application) {
	route := app.MakeRoute()
	route.Get("sms", ***)
}

Migrations

func (receiver *ServiceProvider) Boot(app foundation.Application) {
  app.Publishes("github.com/goravel/example-package", map[string]string{
    "migrations": app.DatabasePath("migrations"),
  })
}

Commands

func (receiver *ServiceProvider) Boot(app foundation.Application) {
	app.Commands([]console.Command{
		commands.NewSmsCommand(),
	})
}

Public Assets

Your package may have assets such as JavaScript, CSS, and images. To publish these assets to the application's public directory, use the service provider's Publishes method:

func (receiver *ServiceProvider) Boot(app foundation.Application) {
  app.Publishes("github.com/goravel/example-package", map[string]string{
    "public": app.PublicPath("vendor"),
  })
}

Publishing File Groups

You may want to publish groups of package assets and resources separately. For instance, you might want to allow your users to publish your package's configuration files without being forced to publish your package's assets. You may do this by "tagging" them when calling the Publishes method from a package's service provider. For example, let's use tags to define two publish groups for the sms package (sms-config and sms-migrations) in the boot method of the package's service provider:

func (receiver *ServiceProvider) Boot(app foundation.Application) {
  app.Publishes("github.com/goravel/example-package", map[string]string{
    "config/sms.go": app.ConfigPath("sms.go"),
  }, "sms-config")
  app.Publishes("github.com/goravel/example-package", map[string]string{
    "migrations": app.DatabasePath("migrations"),
  }, "sms-migrations")
}

Publish Resources

In the project, You can publish the resources registered in a package using vendor:publish Artisan command:

go run . artisan vendor:publish --package={You package name}

The command can use the following options:

Option Name
Alias
Action

--package

-p

Package name, can be a remote package: github.com/goravel/example-package, can also be a local package: ./packages/example-package, note that when using a local package name, it needs to start with ./.

--tag

-t

Resource group

--force

-f

Overwrite any existing files

--existing

-e

Publish and overwrite only the files that have already been published

If there are in your package, you can use app.MakeRoute() to resolve facades.Route(), then add the routes to project:

If there are in your package, you can publish them by the Publishes method:

You can register Artisan command by the Commands method, you can run the commands using after registering them.

goravel/example-package
Service providers
routes
migrations
Artisan CLI