Laravel5.6 多模块配置

  • Post author:
  • Post category:其他



步骤1.打开网址

https://packagist.org/packages/caffeinated/modules


根据php 、 laravel 版本 查看对应的

caffeinated

版本


2.配置composer.根据步骤1.配置


实际上我的配置是

    "require": {
        "php": "^7.1.3",
        "fideloper/proxy": "^4.0",
        "laravel/framework": "5.6.*",
        "laravel/tinker": "^1.0",
        "caffeinated/modules": "4.3.*"
    },


3.执行 composer update


4.更新完成后 在 app/config/app.php中 加入如下2行

4.1  providers  中加入

        /***********多模块配置项*****************/
        Caffeinated\Modules\ModulesServiceProvider::class,

4.2 aliases 中加入

        /***********多模块配置*****************/
        'Module' => Caffeinated\Modules\Facades\Module::class,


5. 执行命令

php artisan vendor:publish

输入带有 Caffeinate 行前面 数字 回车即可


6.生成模块



比如我们想创建一个 Api 模块:php artisan make:module Api

执行命令

php artisan make:module Api

之后 按照如下提示输入即可

Please enter the name of the module: [Api]: > Api

Please enter the slug for the module: [api]: > api

Please enter the module version: [1.0]: > 1.0

Please enter the description of the module: [This is th > application api module Please enter the author of the module: [ ]: > big_cat Please enter the module license: [MIT]: >

You have provided the following manifest information:

Name: Api

Slug: api

Version: 1.0

Description: application api module

Author: big_cat License: MIT

Do you wish to continue? (yes/no) [no]: > yes

Thanks! That’s all we need.

Now relax while your module is generated for you.

0/4 [>—————————] 0%

1/4 [=======>——————–] 25%

2/4 [==============>————-] 50%

3/4 [=====================>——] 75%

4/4 [============================] 100%

Module generated successfully.

创建完成!

这时我们再看下 app 目录的结构

├─Console
│  └─Commands
├─Events
├─Exceptions
├─Http
│  ├─Controllers
│  │  └─Auth
│  ├─Middleware
│  └─Requests
├─Jobs
├─Listeners
├─Modules
│  └─api
│      ├─Console
│      ├─Database
│      │  ├─Migrations
│      │  └─Seeds
│      ├─Http
│      │  ├─Controllers
│      │  ├─Middleware
│      │  └─Requests
│      ├─Providers
│      └─Resources
│          ├─Lang
│          └─Views
├─Policies
└─Providers

可以看到 modules 模块目录下存放着我们新建的 api 模块,拥有自己独立的 mvc 结构’且拥有自己的独立的路由配置:我们可以方便的在此处配置模块的路由

在 app\Mdodules\Api\Routes\web.php

中定义路由

Route::group(['prefix' => 'home'], function () {
    Route::get('/', function () {
        dd('This is the Home module index page. Build something great!');
    });
    Auth::routes();

    Route::get('/index', 'HomeController@index')->name('home');
});

访问 http://domain/api/index 即可访问此模块



版权声明:本文为zhangleia原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。