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
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.
"Modules\\": "Modules/"
should be"Modules\\": "app/Modules/"
instead. And don't forget to docomposer dump-autoload
. Edit: see @Ruffles answer. You already have the Modules folder inside app/. – Doom5