选择一个仓库名称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 中。