diff --git a/config/config.php b/config/config.php old mode 100644 new mode 100755 index 9c72c41d2..4432f45b9 --- a/config/config.php +++ b/config/config.php @@ -26,7 +26,8 @@ 'enabled' => false, 'path' => base_path() . '/vendor/nwidart/laravel-modules/src/Commands/stubs', 'files' => [ - 'routes' => 'Routes/web.php', + 'routes/web' => 'Routes/web.php', + 'routes/api' => 'Routes/api.php', 'views/index' => 'Resources/views/index.blade.php', 'views/master' => 'Resources/views/layouts/master.blade.php', 'scaffold/config' => 'Config/config.php', @@ -37,7 +38,8 @@ 'package' => 'package.json', ], 'replacements' => [ - 'routes' => ['LOWER_NAME', 'STUDLY_NAME', 'MODULE_NAMESPACE'], + 'routes/web' => ['LOWER_NAME', 'STUDLY_NAME'], + 'routes/api' => ['LOWER_NAME'], 'webpack' => ['LOWER_NAME'], 'json' => ['LOWER_NAME', 'STUDLY_NAME', 'MODULE_NAMESPACE'], 'views/index' => ['LOWER_NAME'], diff --git a/phpunit.xml.dist b/phpunit.xml.dist old mode 100644 new mode 100755 diff --git a/src/Commands/RouteProviderMakeCommand.php b/src/Commands/RouteProviderMakeCommand.php index 4efe60535..26a1d19b1 100644 --- a/src/Commands/RouteProviderMakeCommand.php +++ b/src/Commands/RouteProviderMakeCommand.php @@ -53,7 +53,8 @@ protected function getTemplateContents() 'CLASS' => $this->getFileName(), 'MODULE_NAMESPACE' => $this->laravel['modules']->config('namespace'), 'MODULE' => $this->getModuleName(), - 'ROUTES_PATH' => $this->getRoutesPath(), + 'WEB_ROUTES_PATH' => $this->getWebRoutesPath(), + 'API_ROUTES_PATH' => $this->getApiRoutesPath(), 'LOWER_NAME' => $module->getLowerName(), ]))->render(); } @@ -83,11 +84,19 @@ protected function getDestinationFilePath() /** * @return mixed */ - protected function getRoutesPath() + protected function getWebRoutesPath() { return '/' . $this->laravel['config']->get('stubs.files.routes', 'Routes/web.php'); } + /** + * @return mixed + */ + protected function getApiRoutesPath() + { + return '/' . $this->laravel['config']->get('stubs.files.routes', 'Routes/api.php'); + } + public function getDefaultNamespace() : string { return $this->laravel['modules']->config('paths.generator.provider.path', 'Providers'); diff --git a/src/Commands/stubs/route-provider.stub b/src/Commands/stubs/route-provider.stub index db3d3ef0b..b306e895c 100644 --- a/src/Commands/stubs/route-provider.stub +++ b/src/Commands/stubs/route-provider.stub @@ -7,6 +7,13 @@ use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvi class $CLASS$ extends ServiceProvider { + /** + * The root namespace to assume when generating URLs to actions. + * + * @var string + */ + protected $namespace = '$MODULE_NAMESPACE$\$MODULE$\Http\Controllers'; + /** * Called before routes are registered. * @@ -16,6 +23,18 @@ class $CLASS$ extends ServiceProvider */ public function boot() { + parent::boot(); + } + + /** + * Define the routes for the application. + * + * @return void + */ + public function map() + { + $this->mapApiRoutes(); + $this->mapWebRoutes(); } @@ -28,9 +47,23 @@ class $CLASS$ extends ServiceProvider */ protected function mapWebRoutes() { - Route::prefix('$LOWER_NAME$') - ->middleware('web') - ->namespace('$MODULE_NAMESPACE$\$MODULE$\Http\Controllers') + Route::middleware('web') + ->namespace($this->namespace) ->group(__DIR__ . '/../Routes/web.php'); } + + /** + * Define the "api" routes for the application. + * + * These routes are typically stateless. + * + * @return void + */ + protected function mapApiRoutes() + { + Route::prefix('api') + ->middleware('api') + ->namespace($this->namespace) + ->group(__DIR__ . '/../Routes/api.php'); + } } diff --git a/src/Commands/stubs/routes.stub b/src/Commands/stubs/routes.stub deleted file mode 100644 index c0fffb882..000000000 --- a/src/Commands/stubs/routes.stub +++ /dev/null @@ -1,3 +0,0 @@ -get('/$LOWER_NAME$', function (Request $request) { + return $request->user(); +}); \ No newline at end of file diff --git a/src/Commands/stubs/routes/web.stub b/src/Commands/stubs/routes/web.stub new file mode 100755 index 000000000..ab7e7f7c3 --- /dev/null +++ b/src/Commands/stubs/routes/web.stub @@ -0,0 +1,16 @@ +group(function() { + Route::get('/', '$STUDLY_NAME$Controller@index'); +}); diff --git a/tests/Commands/ModuleMakeCommandTest.php b/tests/Commands/ModuleMakeCommandTest.php index 1dd5f72d5..bf60e7e0f 100644 --- a/tests/Commands/ModuleMakeCommandTest.php +++ b/tests/Commands/ModuleMakeCommandTest.php @@ -68,11 +68,23 @@ public function it_generates_module_files() } /** @test */ - public function it_generates_route_file() + public function it_generates_web_route_file() { + $files = $this->app['modules']->config('stubs.files'); $this->artisan('module:make', ['name' => ['Blog']]); - $path = $this->modulePath . '/' . $this->app['modules']->config('stubs.files.routes'); + $path = $this->modulePath . '/' . $files['routes/web']; + + $this->assertMatchesSnapshot($this->finder->get($path)); + } + + /** @test */ + public function it_generates_api_route_file() + { + $files = $this->app['modules']->config('stubs.files'); + $this->artisan('module:make', ['name' => ['Blog']]); + + $path = $this->modulePath . '/' . $files['routes/api']; $this->assertMatchesSnapshot($this->finder->get($path)); } diff --git a/tests/Commands/__snapshots__/ModuleMakeCommandTest__it_generates_api_route_file__1.php b/tests/Commands/__snapshots__/ModuleMakeCommandTest__it_generates_api_route_file__1.php new file mode 100644 index 000000000..2de6fa801 --- /dev/null +++ b/tests/Commands/__snapshots__/ModuleMakeCommandTest__it_generates_api_route_file__1.php @@ -0,0 +1,18 @@ +get(\'/blog\', function (Request $request) { + return $request->user(); +});'; diff --git a/tests/Commands/__snapshots__/ModuleMakeCommandTest__it_generates_module_resources__4.php b/tests/Commands/__snapshots__/ModuleMakeCommandTest__it_generates_module_resources__4.php index f59651e84..c044ad371 100644 --- a/tests/Commands/__snapshots__/ModuleMakeCommandTest__it_generates_module_resources__4.php +++ b/tests/Commands/__snapshots__/ModuleMakeCommandTest__it_generates_module_resources__4.php @@ -2,11 +2,18 @@ namespace Modules\\Blog\\Providers; -use Illuminate\\Support\\Facades\Route; +use Illuminate\\Support\\Facades\\Route; use Illuminate\\Foundation\\Support\\Providers\\RouteServiceProvider as ServiceProvider; class RouteServiceProvider extends ServiceProvider { + /** + * The root namespace to assume when generating URLs to actions. + * + * @var string + */ + protected $namespace = \'Modules\\Blog\\Http\\Controllers\'; + /** * Called before routes are registered. * @@ -16,6 +23,18 @@ class RouteServiceProvider extends ServiceProvider */ public function boot() { + parent::boot(); + } + + /** + * Define the routes for the application. + * + * @return void + */ + public function map() + { + $this->mapApiRoutes(); + $this->mapWebRoutes(); } @@ -28,10 +47,24 @@ public function boot() */ protected function mapWebRoutes() { - Route::prefix(\'blog\') - ->middleware(\'web\') - ->namespace(\'Modules\\Blog\\Http\\Controllers\') + Route::middleware(\'web\') + ->namespace($this->namespace) ->group(__DIR__ . \'/../Routes/web.php\'); } + + /** + * Define the "api" routes for the application. + * + * These routes are typically stateless. + * + * @return void + */ + protected function mapApiRoutes() + { + Route::prefix(\'api\') + ->middleware(\'api\') + ->namespace($this->namespace) + ->group(__DIR__ . \'/../Routes/api.php\'); + } } -'; \ No newline at end of file +'; diff --git a/tests/Commands/__snapshots__/ModuleMakeCommandTest__it_generates_web_route_file__1.php b/tests/Commands/__snapshots__/ModuleMakeCommandTest__it_generates_web_route_file__1.php new file mode 100644 index 000000000..ac0affec8 --- /dev/null +++ b/tests/Commands/__snapshots__/ModuleMakeCommandTest__it_generates_web_route_file__1.php @@ -0,0 +1,17 @@ +group(function() { + Route::get(\'/\', \'BlogController@index\'); +}); +'; diff --git a/tests/Commands/__snapshots__/RouteProviderMakeCommandTest__it_can_change_the_default_namespace__1.php b/tests/Commands/__snapshots__/RouteProviderMakeCommandTest__it_can_change_the_default_namespace__1.php index 2f526ed37..5a566eb1b 100644 --- a/tests/Commands/__snapshots__/RouteProviderMakeCommandTest__it_can_change_the_default_namespace__1.php +++ b/tests/Commands/__snapshots__/RouteProviderMakeCommandTest__it_can_change_the_default_namespace__1.php @@ -7,6 +7,13 @@ class RouteServiceProvider extends ServiceProvider { + /** + * The root namespace to assume when generating URLs to actions. + * + * @var string + */ + protected $namespace = \'Modules\\Blog\\Http\\Controllers\'; + /** * Called before routes are registered. * @@ -16,6 +23,18 @@ class RouteServiceProvider extends ServiceProvider */ public function boot() { + parent::boot(); + } + + /** + * Define the routes for the application. + * + * @return void + */ + public function map() + { + $this->mapApiRoutes(); + $this->mapWebRoutes(); } @@ -28,10 +47,24 @@ public function boot() */ protected function mapWebRoutes() { - Route::prefix(\'blog\') - ->middleware(\'web\') - ->namespace(\'Modules\\Blog\\Http\\Controllers\') + Route::middleware(\'web\') + ->namespace($this->namespace) ->group(__DIR__ . \'/../Routes/web.php\'); } + + /** + * Define the "api" routes for the application. + * + * These routes are typically stateless. + * + * @return void + */ + protected function mapApiRoutes() + { + Route::prefix(\'api\') + ->middleware(\'api\') + ->namespace($this->namespace) + ->group(__DIR__ . \'/../Routes/api.php\'); + } } '; diff --git a/tests/Commands/__snapshots__/RouteProviderMakeCommandTest__it_generated_correct_file_with_content__1.php b/tests/Commands/__snapshots__/RouteProviderMakeCommandTest__it_generated_correct_file_with_content__1.php index 628fcde30..c044ad371 100644 --- a/tests/Commands/__snapshots__/RouteProviderMakeCommandTest__it_generated_correct_file_with_content__1.php +++ b/tests/Commands/__snapshots__/RouteProviderMakeCommandTest__it_generated_correct_file_with_content__1.php @@ -7,6 +7,13 @@ class RouteServiceProvider extends ServiceProvider { + /** + * The root namespace to assume when generating URLs to actions. + * + * @var string + */ + protected $namespace = \'Modules\\Blog\\Http\\Controllers\'; + /** * Called before routes are registered. * @@ -16,6 +23,18 @@ class RouteServiceProvider extends ServiceProvider */ public function boot() { + parent::boot(); + } + + /** + * Define the routes for the application. + * + * @return void + */ + public function map() + { + $this->mapApiRoutes(); + $this->mapWebRoutes(); } @@ -28,10 +47,24 @@ public function boot() */ protected function mapWebRoutes() { - Route::prefix(\'blog\') - ->middleware(\'web\') - ->namespace(\'Modules\\Blog\\Http\\Controllers\') + Route::middleware(\'web\') + ->namespace($this->namespace) ->group(__DIR__ . \'/../Routes/web.php\'); } + + /** + * Define the "api" routes for the application. + * + * These routes are typically stateless. + * + * @return void + */ + protected function mapApiRoutes() + { + Route::prefix(\'api\') + ->middleware(\'api\') + ->namespace($this->namespace) + ->group(__DIR__ . \'/../Routes/api.php\'); + } } ';