Skip to content

Commit

Permalink
Merge pull request #633 from arthvrian/api-routes
Browse files Browse the repository at this point in the history
API routes
  • Loading branch information
nWidart authored Sep 25, 2018
2 parents d44d481 + c6ea8a2 commit c2034ee
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 23 deletions.
6 changes: 4 additions & 2 deletions config/config.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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'],
Expand Down
Empty file modified phpunit.xml.dist
100644 → 100755
Empty file.
13 changes: 11 additions & 2 deletions src/Commands/RouteProviderMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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');
Expand Down
39 changes: 36 additions & 3 deletions src/Commands/stubs/route-provider.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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();
}

Expand All @@ -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');
}
}
3 changes: 0 additions & 3 deletions src/Commands/stubs/routes.stub

This file was deleted.

18 changes: 18 additions & 0 deletions src/Commands/stubs/routes/api.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::middleware('auth:api')->get('/$LOWER_NAME$', function (Request $request) {
return $request->user();
});
16 changes: 16 additions & 0 deletions src/Commands/stubs/routes/web.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::prefix('$LOWER_NAME$')->group(function() {
Route::get('/', '$STUDLY_NAME$Controller@index');
});
16 changes: 14 additions & 2 deletions tests/Commands/ModuleMakeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php return '<?php
use Illuminate\\Http\\Request;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::middleware(\'auth:api\')->get(\'/blog\', function (Request $request) {
return $request->user();
});';
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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();
}
Expand All @@ -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\');
}
}
';
';
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php return '<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::prefix(\'blog\')->group(function() {
Route::get(\'/\', \'BlogController@index\');
});
';
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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();
}
Expand All @@ -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\');
}
}
';
Loading

0 comments on commit c2034ee

Please sign in to comment.