dslk6326846
2016-07-14 07:44
浏览 51
已采纳

在Laravel 5.2外部应用程序上创建模块

I'm currently creating a modular laravel project here was my reference link

Creating a Modular Application in Laravel

Right now I want to create a module folder same directory with my app folder

Folder Structure

I also create a ModuleServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class ModuleServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {

        $modules = config('modules');

        if (ENVIRONMENT != 'local') {

        } else {

            foreach ($modules as $key => $module) {

                // Load the routes for each of the modules
                if(file_exists(__DIR__ . "../../../modules/{$module}/routes.php")) {
                    require __DIR__ . "../../../modules/{$module}/routes.php";
                } else {
                    echo "{$module} Module Not found"; die;
                }

                // Load the views
                if(is_dir(__DIR__ . "../../../modules/{$module}/views")) {
                    $this->loadViewsFrom(__DIR__ . "../../../modules/{$module}/views", $module);
                } else {
                    echo  "{$module} Module View Not found"; die;
                }
            }

        }

    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

I added the ModulerServiceProvider class to my

config/app.php

providers' => [
    App\Providers\ModuleServiceProvider::class,
]

and here is the route under modules/yinyang/route.php being added

Route::group(['prefix' => 'yinyang', 'namespace' => 'Modules\Yinyang\Controllers'], function () {

    Route::get('/', 'GameController@index');
});

Here is the modules/yinyang/controller/GameController.php

namespace Modules\Yinyang\Controllers;

use App\Http\Controllers;

class GameController extends Controller {


    public function __construct() {

    }

    public function index() {

    }

}

And Here is the problem when i am trying to access already the route under yinyang module http://192.168.200.30/try/sample/public/yinyang

Class Modules\Yinyang\Controllers\GameController does not exist

Anyone has any advice/suggestion on how to solve this problem. Thanks.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongqiong8021 2016-07-14 08:20
    已采纳

    I solve the problem with the help of

    Modular Structure Laravel 5

    For me to access the module directory I need to add new instance on my composer.json Modules to the psr-4

    "psr-4": {
        "App\\": "app/",
        "Modules\\": "modules/"
    }
    

    After that run composer dump-autoload to create a new build.

    点赞 打赏 评论

相关推荐 更多相似问题