好久沒寫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 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 專案引入本地擴充包
-
編輯 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... */ JesseyChen\PackageTest\TestServiceProvider::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 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'),
]);
}
}
-
釋出配置檔案
php artisan vendor:publish
- 編寫 Test.php
<?php
namespace JesseyChen\PackageTest;
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 JesseyChen\PackageTest;
use \Illuminate\Support\Facades\Facade as LaravelFacade;
class Facade extends LaravelFacade
{
protected static function getFacadeAccessor()
{
return 'test';
}
}
- 在 app/config 目錄註冊Facade類
'aliases' => [ ...., 'Test' => JesseyChen\PackageTest\Facade::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 require "jesseychen/package-test:1.0"