Skip to content

Phát triển Package (Package Development)

Giới thiệu (Introduction)

Packages là cách chính để thêm tính năng cho Laravel. Packages có thể là bất cứ thứ gì — từ date library như Carbon đến framework testing như Pest.

Có nhiều loại packages: standalone (hoạt động với bất kỳ PHP framework nào) hoặc Laravel-specific (tận dụng service container, routes, views, v.v.).

Package Discovery

Service provider tự động được phát hiện nhờ extra.laravel trong composer.json:

json
"extra": {
    "laravel": {
        "providers": [
            "Barryvdh\\Debugbar\\ServiceProvider"
        ],
        "aliases": {
            "Debugbar": "Barryvdh\\Debugbar\\Facades\\Debugbar"
        }
    }
}

Service Providers

Service Provider là điểm kết nối giữa package và Laravel. Đăng ký bindings trong service container:

php
<?php

namespace Vendor\Package;

use Illuminate\Support\ServiceProvider;

class PackageServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->app->singleton(MyService::class, function ($app) {
            return new MyService($app['config']['package']);
        });
    }

    public function boot(): void
    {
        // Đăng ký resources...
    }
}

Resources

Configuration

Publish config file:

php
public function boot(): void
{
    $this->publishes([
        __DIR__.'/../config/courier.php' => config_path('courier.php'),
    ]);
}

public function register(): void
{
    $this->mergeConfigFrom(
        __DIR__.'/../config/courier.php', 'courier'
    );
}

Routes

php
public function boot(): void
{
    $this->loadRoutesFrom(__DIR__.'/../routes/web.php');
}

Migrations

php
public function boot(): void
{
    $this->loadMigrationsFrom(__DIR__.'/../database/migrations');

    // Hoặc publish
    $this->publishesMigrations([
        __DIR__.'/../database/migrations' => database_path('migrations'),
    ]);
}

Language Files

php
public function boot(): void
{
    $this->loadTranslationsFrom(__DIR__.'/../lang', 'courier');

    $this->publishes([
        __DIR__.'/../lang' => $this->app->langPath('vendor/courier'),
    ]);
}

Sử dụng: __('courier::messages.welcome')

Views

php
public function boot(): void
{
    $this->loadViewsFrom(__DIR__.'/../resources/views', 'courier');

    $this->publishes([
        __DIR__.'/../resources/views' => resource_path('views/vendor/courier'),
    ]);
}

Sử dụng: view('courier::admin.dashboard')

View Components

php
use Illuminate\Support\Facades\Blade;

public function boot(): void
{
    Blade::componentNamespace('Courier\\Views\\Components', 'courier');
}

Sử dụng: <x-courier::alert />

Commands

php
public function boot(): void
{
    if ($this->app->runningInConsole()) {
        $this->commands([
            InstallCommand::class,
            NetworkCommand::class,
        ]);
    }
}

Public Assets

Publish assets (CSS, JS, images):

php
public function boot(): void
{
    $this->publishes([
        __DIR__.'/../public' => public_path('vendor/courier'),
    ], 'courier-assets');
}

Publishing File Groups

Chia publish thành groups:

php
$this->publishes([
    __DIR__.'/../config/package.php' => config_path('package.php'),
], 'courier-config');

$this->publishes([
    __DIR__.'/../database/migrations/' => database_path('migrations'),
], 'courier-migrations');

Publish group cụ thể:

bash
php artisan vendor:publish --tag=courier-config