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 为拓展包的包名
Step3. 实现拓展包的内容
Test.php
<?php
namespace JesseyChenPackageTest;
class Test
{
public function execute()
{
echo 'this is a test. ';
}
}
TestServiceProvider.php
<?php
namespace JesseyChenPackageTest;
use IlluminateSupportServiceProvider;
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 项目引入本地拓展包
-
编辑 laravel 项目文件的 composer.json
.... , "repositories": { "jesseyChen": { "type": "path", "url": "/var/www/laravel-package/app/packages/jesseychen/package-test" } } # 注意 “url” 为绝对路径
-
安装拓展包
composer require jesseychen/package-test:dev-master
安装完会发现 jesseychen/package-test
出现在 laravel/vendor
此时编辑 app/packages/jesseychen/package-test
目录内的文件,laravel/vendor/jesseychen/package-test
目录的文件会跟着改变,相当做了文件映射,这样方便本地调试
-
把 service 注册到 laravel 项目中,在
config/app.php
添加'providers' => [ /* * Laravel Framework Service Providers... */ .... /* * Package Service Providers... */ JesseyChenPackageTestTestServiceProvider::class, /* * Application Service Providers... */ .... ],
Step5. 测试
- 编辑 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();
});
添加配置文件 config
- 在 src 下创建 config 目录来存取我们的配置参数文件 config/test.php
<?php
return [
'key' => 'jessey',
'value' => 10
];
- 编辑 TestServiceProvider.php.
<?php
namespace JesseyChenPackageTest;
use IlluminateSupportServiceProvider;
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'),
]);
}
}
-
发布配置文件
php artisan vendor:publish
-
编写 Test.php
<?php
namespace JesseyChenPackageTest;
class Test
{
public function execute()
{
echo 'This is a test..';
}
public function getConfig()
{
var_dump(config('test'));
}
}
- 测试
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
- src 目录下创建 Facade.php
<?php
namespace JesseyChenPackageTest;
use IlluminateSupportFacadesFacade as LaravelFacade;
class Facade extends LaravelFacade
{
protected static function getFacadeAccessor()
{
return 'test';
}
}
- 在 app/config 目录注册 Facade 类
'aliases' => [ ...., 'Test' => JesseyChenPackageTestFacade::class ],
- 测试,
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/
- 点击右上角的 submit
- 把 github 上 https 的链接 cv 后 check
- 成功后,出现下图
以后更新拓展包,把本地新代码提交到 github 后,点击图上的 update 同步最新的代码
3. 测试
先删除本地的包
- 删除 composer.json 的 "repositories"、"require" 的 "jesseychen/package-test": "dev-master",
- 注释掉 config/app.php 内刚刚添加的 providers 和 aliases
- 执行 composer update
安装
composer clear-cache
composer require "jesseychen/package-test:1.0"
本作品采用《CC 协议》,转载必须注明作者和本文链接