Laravel 搭建 Composer 包,实现配置 Config、门面 Facade、服务 Service、发布到 Packagist

好久没写php代码,有需求要写一个内部使用的包,折腾了半天,用最简单的代码写下来备份一下流程

Step1. 初始化laravel项目

composer create-project laravel/laravel laravel-package

Step2. 创建目录,初始化 composer.json

➜  mkdir -p  app/packages/jesseychen/package-test  
➜  cd app/packages/jesseychen/package-test
➜  composer init
➜  mkdir src
➜  touch README.md
➜  cd src
➜  touch Test.php TestServiceProvider.php
#  jesseychen是用户名,表示谁的包
#  package-test 为拓展包的包名

123

Step3. 实现拓展包的内容

Test.php

<?php

namespace JesseyChen\PackageTest;

class Test
{
    public function execute()
    {
        echo 'this is a test. ';
    }
}

TestServiceProvider.php

<?php

namespace JesseyChen\PackageTest;

use Illuminate\Support\ServiceProvider;

class TestServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->singleton('test', function () {
            return new Test();
        });
    }
}

composer.json

{
    "name": "jesseychen/package-test",
    "description": "This is a test.",
    "type": "library",
    "license": "MIT",
    "authors": [
        {
            "name": "summer",
            "email": "chenjunxing1012@gmail.com"
        }
    ],
    "minimum-stability": "dev",
    "require": {},
    "autoload": {
        "psr-4": {
            "JesseyChen\\PackageTest\\": "src/"
        }
    }
}

Step4. 在 laravel 项目引入本地拓展包

  1. 编辑 laravel 项目文件的 composer.json

    .... ,
    "repositories": {
        "jesseyChen": {
            "type": "path",
            "url": "/var/www/laravel-package/app/packages/jesseychen/package-test"
        }
    }
    # 注意 “url” 为绝对路径
  2. 安装拓展包

    composer require jesseychen/package-test:dev-master

安装完会发现 jesseychen/package-test 出现在 laravel/vendor

123

此时编辑app/packages/jesseychen/package-test目录内的文件,laravel/vendor/jesseychen/package-test目录的文件会跟着改变,相当做了文件映射,这样方便本地调试

  1. 把service 注册到 laravel 项目中,在 config/app.php 添加

    'providers' => [
    
        /*
         * Laravel Framework Service Providers...
         */
         ....
    
        /*
         * Package Service Providers...
         */
        JesseyChen\PackageTest\TestServiceProvider::class,
        /*
         * Application Service Providers...
         */
        ....
    ],

Step5. 测试

  1. 编辑 route/web.php
<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    app('test')->execute();
});

123


添加配置文件 config

  1. 在src下创建config 目录来存取我们的配置参数文件 config/test.php
<?php
return [
    'key' => 'jessey',
    'value' => 10
];
  1. 编辑TestServiceProvider.php.
<?php

namespace JesseyChen\PackageTest;

use Illuminate\Support\ServiceProvider;

class TestServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->singleton('test', function () {
            return new Test();
        });
    }

    public function boot()
    {
        $this->publishes([
            __DIR__ . '/config/test.php' => config_path('test.php'),
        ]);
    }
}
  1. 发布配置文件 php artisan vendor:publish
    123

  2. 编写 Test.php

<?php

namespace JesseyChen\PackageTest;

class Test
{

    public function execute()
    {
        echo 'This is a test..';
    }

    public function getConfig()
    {
        var_dump(config('test'));
    }
}
  1. 测试 vim route/web.php
<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    app('test')->getConfig();
});

添加门面 Facade

  1. src目录下创建Facade.php
<?php

namespace JesseyChen\PackageTest;

use \Illuminate\Support\Facades\Facade as LaravelFacade;

class Facade extends LaravelFacade
{
    protected static function getFacadeAccessor()
    {
        return 'test';
    }
}
  1. 在 app/config 目录注册Facade类
    'aliases' => [
        ....,
        'Test' => JesseyChen\PackageTest\Facade::class
    ],
  2. 测试,vim route/web.php
<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    Test::execute();
});

发布拓展包到 https://packagist.org

1. 首选把 package-test包 上传到 github 或者 coding

➜  cd app/packages/jesseychen/package-test 
➜  git init
➜  git add .
➜  git commit -m 'init package'
➜  git remote add origin https://github.com/JesseyChen/package-test.git
➜  git push -u origin master
➜  git tag 1.0
➜  git push --tag

vim package-test/composer.json ,添加 "version"

{
    ...
    "version": "1.0",
}

2. 上传到 https://packagist.org/

  1. 点击右上角的submit
    123
  2. 把 github 上https的链接cv后 check
    123
  3. 成功后,出现下图
    123
    以后更新拓展包,把本地新代码提交到github后,点击图上的 update同步最新的代码

3. 测试

先删除本地的包

  1. 删除 composer.json 的 "repositories"、"require" 的 "jesseychen/package-test": "dev-master",
  2. 注释掉 config/app.php 内刚刚添加的providers和aliases
  3. 执行 composer update

安装

composer clear-cache 
composer require "jesseychen/package-test:1.0"

Laravel 搭建 Composer 包,实现配置 Config、门面 Facade、服务 Service、发布到 packagist

本作品采用《CC 协议》,转载必须注明作者和本文链接
本帖由系统于 7个月前 自动加精
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 6

学习了

8个月前 评论
Krisji

easy

7个月前 评论

今天正好发现一个用于快速开发laravel package的包 : https://github.com/ConsoleTVs/Bootpack
前来学习

7个月前 评论

再配个服务自动发现就更完美了。

5个月前 评论

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!