0
votes

I know there are question/answer for routing in modules. But i have tried all that but i can't get any workaround.

Let me show what i have done.

My folder structure

enter image description here

My ModuleServiceProvider.php

namespace App\Modules;

class ModulesServiceProvider extends \Illuminate\Support\ServiceProvider
{
    /**
     * Will make sure that the required modules have been fully loaded
     * @return void
     */

    public function boot()
    {

        if ($module = $this->getModule(func_get_args())) {
             include __DIR__.'/'.$module.'/routes.php';
        }
        $this->loadViewsFrom(__DIR__.'/'.$module.'/Views', 'core');
    }

    public function register()
    {
        if ($module = $this->getModule(func_get_args())) {

        }
    }

    public function getModule($args)
    {
        $module = (isset($args[0]) and is_string($args[0])) ? $args[0] : null;

        return $module;
    }
}

My BlogServiceProvider.php

namespace App\Modules\Blog;

class BlogServiceProvider extends \App\Modules\ModulesServiceProvider {

    public function register()
    {
        parent::register('Blog');
    }

    public function boot()
    {
        parent::boot('Blog');
    }
}

in app\config\app.php

'App\Modules\Blog\BlogServiceProvider', //added in providers array

in app\config

module.php created

return  [
    'modules' => [
       'Blog',
    ]
];

in composer.json

"autoload": {
        "classmap": [
            "database",
            "app/Modules"
        ],
        "psr-4": {
            "App\\": "app/",
             "Modules\\": "Modules/"
        }
    },

Everything seems perfect but in i trapped in routes.

in my app\Modules\Blog\routes.php

    Route::group(['namespace' => array('Modules\Blog')], function() {
      Route::get('/',['as' => 'home', 'uses' => 'PostController@index']);
    });
Route::get('/', ['as' => 'home', 'uses' => 'App\Modules\Blog\Controllers\PostController@index']);

I m getting PostController doesn't exist error

My controller namespace

 namespace App\Modules\Blog\Controllers;

I am getting that error on both route. whether i specify controller path or not. any help please. How to route in modular application.

5
I think this line on your composer.json: "Modules\\": "Modules/" should be "Modules\\": "app/Modules/"instead. And don't forget to do composer dump-autoload. Edit: see @Ruffles answer. You already have the Modules folder inside app/.Doom5
As per documentation of laravel-modules, it is instructed to have Modules directory as a sibling of app directory, not its child. Coz the default configurations of the namespaces in the modules is set to derive that way and changing it shall result in specifying custom namespaces to each module in its configuration.Vaishnav Mhetre

5 Answers

0
votes

Try changing the namespace in the route group from ['namespace' => array('Modules\Blog')] to ['namespace' => 'App\Modules\Blog\Controllers'] and see if it works.

0
votes

1 Edit your composer.json file and correct your prs-4 autoload to:

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

2 update the namespaces of your files by removing the "App\" and 3 do a composer dump-autoload

0
votes

Go to the map method on your RouteServiceProvider. You can tell laravel to load your routes depending on the namespace.

$router->group(['namespace' => 'App\Controllers'], function ($router)   {
        require app_path('Http/routes.php');
    });

$router->group(['namespace' => 'App\Modules\MyModuleControllers'], function ($router) {
        require app_path('Modules/MyModule/routes.php');
    });
0
votes

Finally i got solution by this. i changed ModuleServiceProvider

namespace App\Modules;

class ModulesServiceProvider extends \Illuminate\Support\ServiceProvider
{
    /**
     * Will make sure that the required modules have been fully loaded
     * @return void
     */

    public function boot()
    {

        if ($module = $this->getModule(func_get_args())) {
             require_once __DIR__.'/'.$module.'/routes.php';
        }
        $this->loadViewsFrom(__DIR__.'/'.$module.'/Views', 'core');
    }

    public function register()
    {
        if ($module = $this->getModule(func_get_args())) {

        }
    }

    public function getModule($args)
    {
        $module = (isset($args[0]) and is_string($args[0])) ? $args[0] : null;

        return $module;
    }
}

I replaced require_once instead of include in boot function. And its working.

In route i am giving

Route::get('new-post',['uses' => 'App\Modules\Blog\Controllers\PostController@create']);

Hope it helps others !!