選擇一個倉庫名稱php
mkdir prirepos
進入目錄laravel
cd prirepos
使用 composer init
初始化私有化倉庫git
composer init
一路回車,或者填寫你想要的參數或者組件包json
{ "name": "chenghuiyong/prirepos", "authors": [ { "name": "會勇禾口王", "email": "chenghuiyong1987@gmail.com" } ], "require": {} }
這裏,參考Laravel 框架組件化實現,舉例實際應用場景:若是你的項目正在使用migration 管理數據遷移和模型文件,當項目越大,數據遷移和模型文件就會變得愈來愈多,忽然一天,另一個項目也想使用該項目中的模型文件,本辦法就是複製粘貼。若是有1000個文件,那麼你複製1000次...安全
下面推薦使用私有化倉庫的方法,既作到共用,也保障安全,而後經過composer 引入到新的項目中。bash
此時,咱們私有倉庫就會變成這樣的結構設計composer
- .git - database - migrations - 2014_10_12_100000_create_users_table - src - Models - User.php - ServiceProvider.php - vendor - composer.json - composer.lock
因而,你的composer.json
可能就會這樣框架
{ "name": "prirepos/model", "description": "The Prirepos Laravel Models", "type": "library", "require": { "illuminate/database": "^6.0|^7.0", "illuminate/support": "^6.0|^7.0", "illuminate/console": "^6.0|^7.0", }, "license": "MIT", "authors": [ { "name": "會勇禾口王", "email": "chenghuiyong1987@gmail.com" } ], "autoload": { "psr-4": { "Prirepos\\Model\\": "src/" } }, "extra": { "laravel": { "providers": [ "Prirepos\\Model\\ServiceProvider" ] } }, "config": { "sort-packages": true }, "minimum-stability": "dev", "prefer-stable": true, "repositories": { "packagist": { "type": "composer", "url": "https://mirrors.aliyun.com/composer/" } } }
重要的ServiceProvider.php
包含dom
<?php namespace Prirepos\Model; use Laravel\Lumen\Application as LumenApplication; use Illuminate\Foundation\Application as LaravelApplication; use Illuminate\Support\ServiceProvider as LaravelServiceProvider; class ServiceProvider extends LaravelServiceProvider { /** * Bootstrap. */ public function boot() { $this->registerMigrations(); $this->publishes([ __DIR__.'/../database/migrations' => database_path('migrations'), ], 'migrations'); } /** * Register Prirepos's migration files. * * @return void */ protected function registerMigrations() { return $this->loadMigrationsFrom(__DIR__.'/../database/migrations'); } }
最後,你的模型文件的命名空間就必定要變成ssh
<?php namespace Prirepos\Model; use Illuminate\Database\Eloquent\Model; class User extends Model { }
優化後,將代碼提交到Gitab倉庫中,好比:git@your_doman:prirepos/model.git
$ git commit -am "init" $ git remote add origin git@your_doman:prirepos/model.git $ git push origin -u master
一般,Composer組件都是放到Packgist這樣的公有平臺,若是要使用私有組件,就須要修改composer.json
。
若是你的倉庫沒有使用https
,就選擇用ssh
方式克隆,不過,須要將id_rsa,pub
提早添加到Gitlab中。
在respositoies
中添加你的私有倉庫地址,
"repositories": { "packagist": { "type": "composer", "url": "https://mirrors.aliyun.com/composer/" }, "prirepos/model": { "type": "vcs", "url": "`git@your_doman:prirepos/model.git" } }
在require
中添加組件名稱
"require": { "prirepos/model": "dev-master" },
使用composer update -vvv
命令安裝組件並使用。
- php artisan vendor:publish --tag=migrations
命令將數據遷移提早發佈。ssh
克隆倉庫,必定要將克隆倉庫用戶的id_rsa,pub
文件添加到Gitlab 中。