当我们开发laravel 扩展package时,有时会涉及到composer.json内容的修改,比方说我原先的composer.json文件内容如下
{
"name": "renrenfo/plus-blesses",
"description": "blesses package.",
"type": "library",
"version":"0.1.0",
"license": "MIT",
"require": {
"php": ">=7.1.3",
"maatwebsite/excel": "^3.0"
},
"autoload": {
"files": [
"src/helpers.php"
],
"psr-4": {
"Renrenfo\\PlusBlesses\\": "src/",
"Renrenfo\\PlusBlesses\\Seeds\\": "database/seeds/"
}
},
"config": {
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
"Renrenfo\\PlusBlesses\\Providers\\AppServiceProvider",
"Renrenfo\\PlusBlesses\\Providers\\ModelServiceProvider",
"Renrenfo\\PlusBlesses\\Providers\\RouteServiceProvider"
]
}
}
}
但是因业务需要我需要加载一款基于PHPExcel开发的Laravel框架专用的Excel/CSV 文件导入导出功能的扩展包Laravel Excel ,需要三步,首先是将扩展包加入进来
1.在package包根目录下Composer安装依赖:
composer require maatwebsite/excel
2.注册服务
'providers'=>[/*
* Package Service Providers...
*/Maatwebsite\Excel\ExcelServiceProvider::class,
]
3.添加Facade门面别名
'aliases'=>[
'Excel'=>Maatwebsite\Excel\Facades\Excel::class,
]
操作之后的composer.json文件如下
{
"name": "renrenfo/plus-blesses",
"description": "blesses package.",
"type": "library",
"version":"0.1.0",
"license": "MIT",
"require": {
"php": ">=7.1.3",
"maatwebsite/excel": "^3.0"
},
"autoload": {
"files": [
"src/helpers.php"
],
"psr-4": {
"Renrenfo\\PlusBlesses\\": "src/",
"Renrenfo\\PlusBlesses\\Seeds\\": "database/seeds/"
}
},
"config": {
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
"Renrenfo\\PlusBlesses\\Providers\\AppServiceProvider",
"Renrenfo\\PlusBlesses\\Providers\\ModelServiceProvider",
"Renrenfo\\PlusBlesses\\Providers\\RouteServiceProvider",
"Maatwebsite\\Excel\\ExcelServiceProvider"
],
"aliases": {
"Excel": "Maatwebsite\\Excel\\Facades\\Excel"
}
}
}
}
注意加粗的地方为改动的地方.然后执行执行
composer update
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"
命令即可. 如果执行该操作后服务没有生效,那么先解除扩展,再加载扩展.
接下来针对此案例执行
composer remove renrenfo/plus-blesses
composer require renrenfo/plus-blesses
composer update
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"