Laravel 本地扩展包开发详细过程

安装 Laravel

laravel new laravel

在根目录创建扩展包专用目录 packages

创建扩展包

1、新建 packages/MyVendor/MyPackage/src

2、在 packages/MyVendor/MyPackage 初始化 composer

D:\www\laravel\packages\MyVendor\MyPackage>composer init


  Welcome to the Composer config generator



This command will guide you through creating your composer.json config.

Package name (<vendor>/<name>) [a.wei/my-package]: myvendor/mypackage
Description []: a package
Author [, n to skip]: A.wei <lzw22@qq.com>
Minimum Stability []: dev
Package Type (e.g. library, project, metapackage, composer-plugin) []: project
License []: MIT

Define your dependencies.

Would you like to define your dependencies (require) interactively [yes]? no
Would you like to define your dev dependencies (require-dev) interactively [yes]? no

{
    "name": "myvendor/mypackage",
    "description": "a package",
    "type": "project",
    "license": "MIT",
    "authors": [
        {
            "name": "A.wei",
            "email": "lzw22@qq.com"
        }
    ],
    "minimum-stability": "dev",
    "require": {}
}

Do you confirm generation [yes]? yes

以上执行后,将生成扩展包 composer.json 文件

3、新建服务提供者,执行

php artisan make:provider MyPackageServiceProvider 

将在 App/Providers 新增 MyPackageServiceProvider 文件,将其移动到 packages/MyVendor/MyPackage/src 下,将修改命名空间:namespace MyVendor\MyPackage;

4、新建控制器 packages/MyVendor/MyPackage/src/MyPackageController.php

<?php

namespace MyVendor\MyPackage;

use App\Http\Controllers\Controller;

class MyPackageController extends Controller
{
    public function index()
    {
        dd('test');
    }
}

5、新建路由文件 packages/MyVendor/MyPackage/src/routes.php

<?php

Route::get('/test', 'MyVendor\MyPackage\MyPackageController@index');

6、在 MyPackageServiceProvider 中注册路由

<?php

namespace MyVendor\MyPackage;

use Illuminate\Support\ServiceProvider;

class MyPackageServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //
        $this->loadRoutesFrom(__DIR__ . '/routes.php');
    }
}

一个扩展包基本功能已完成,现在需要在 laravel 项目中引用该包

引入扩展包

两种方式:一种是 laravel 5.5 之前的手工配置,另外一种是基于 laravel 5.5+ 包自动发现

1、手工配置

在 config/app.php 的 providers 中添加

Packages\MyVendor\MyPackageServiceProvider::class,

修改根目录 composer.json

"autoload": {
    "psr-4": {
        ...
        "MyVendor\\MyPackage":"packages/MyVendor/MyPackage/src"
    },

完成后,执行下面命令,更新自动加载配置

composer dumpautoload

2、composer 安装,包自动发现

修改根目录 composer.json

"require": {
    ...
    "myvendor/mypackage": "dev-master"
},
"repositories": {
    "myvendor/mypackage": {
        "type": "path",
        "url": "packages/MyVendor/MyPackage"
    }
}

配置 laravel 包自动发现,修改 packages/MyVendor/MyPackage/composer.json

...
"require": {},
"autoload": {
    "psr-4": {
        "MyVendor\\MyPackage\\": "src/"
    }
},
"extra": {
    "laravel": {
        "providers": [
            "MyVendor\\MyPackage\\MyPackageServiceProvider"
        ]
    }
}

完成后,执行安装扩展包命令

composer update

测试

在浏览器打开 localhost/test 输出

test

laravel 扩展包生成器

以上都是手工执行创建扩展包,也可以使用 Laravel扩展包生成器 Laravel Packager 来自动生成扩展包

发表评论

电子邮件地址不会被公开。 必填项已用*标注