From bcc6be0d29b6692691a78072861cb2f8cbc149e1 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Thu, 8 Feb 2018 19:09:36 -0600 Subject: [PATCH] Updates for addon/modules to make it easier to create APIs --- composer.json | 1 + config/modules.php | 14 ++++++--- .../{ => Admin}/AdminController.php | 0 .../Http/Controllers/Api/SampleController.php | 31 +++++++++++++++++++ modules/Sample/Http/Routes/admin.php | 7 +++++ modules/Sample/Http/Routes/api.php | 17 ++++++++++ modules/Sample/Http/Routes/web.php | 9 ++++++ modules/Sample/Http/routes.php | 18 ----------- modules/Sample/Models/SampleTable.php | 11 +++++++ .../Providers/SampleServiceProvider.php | 31 ++++++++++++++++++- modules/Sample/composer.json | 9 +++--- resources/stubs/modules/composer.stub | 10 ++++-- resources/stubs/modules/controller-api.stub | 31 +++++++++++++++++++ resources/stubs/modules/model.stub | 5 +-- resources/stubs/modules/routes-admin.stub | 7 +++++ resources/stubs/modules/routes-api.stub | 17 ++++++++++ resources/stubs/modules/routes.stub | 11 +------ .../stubs/modules/scaffold/provider.stub | 31 ++++++++++++++++++- 18 files changed, 216 insertions(+), 44 deletions(-) rename modules/Sample/Http/Controllers/{ => Admin}/AdminController.php (100%) create mode 100644 modules/Sample/Http/Controllers/Api/SampleController.php create mode 100644 modules/Sample/Http/Routes/admin.php create mode 100644 modules/Sample/Http/Routes/api.php create mode 100644 modules/Sample/Http/Routes/web.php delete mode 100644 modules/Sample/Http/routes.php create mode 100644 modules/Sample/Models/SampleTable.php create mode 100644 resources/stubs/modules/controller-api.stub create mode 100644 resources/stubs/modules/routes-admin.stub create mode 100644 resources/stubs/modules/routes-api.stub diff --git a/composer.json b/composer.json index 2b5fbf76..67d7f24c 100755 --- a/composer.json +++ b/composer.json @@ -32,6 +32,7 @@ "league/iso3166": "2.1.x", "google/apiclient": "2.2.x", "theiconic/php-ga-measurement-protocol": "2.7.x", + "joshbrw/laravel-module-installer": "0.1.x", "irazasyed/laravel-gamp": "1.3.x", "vierbergenlars/php-semver": "3.0.x" }, diff --git a/config/modules.php b/config/modules.php index fa118b64..6e66ce66 100644 --- a/config/modules.php +++ b/config/modules.php @@ -1,27 +1,29 @@ 'Modules', - 'stubs' => [ 'enabled' => true, 'path' => resource_path() . '/stubs/modules', 'files' => [ - 'routes' => 'Http/routes.php', + 'routes' => 'Http/Routes/web.php', + 'routes-api' => 'Http/Routes/api.php', + 'routes-admin' => 'Http/Routes/admin.php', 'event-service-provider' => 'Providers/EventServiceProvider.php', 'views/index' => 'Resources/views/index.blade.php', 'views/index-admin' => 'Resources/views/admin/index.blade.php', 'views/frontend' => 'Resources/views/layouts/frontend.blade.php', 'views/admin' => 'Resources/views/layouts/admin.blade.php', 'listener-test' => 'Listeners/TestEventListener.php', - 'controller-admin' => 'Http/Controllers/AdminController.php', + 'controller-api' => 'Http/Controllers/Api/SampleController.php', + 'controller-admin' => 'Http/Controllers/Admin/AdminController.php', 'scaffold/config' => 'Config/config.php', 'composer' => 'composer.json', ], 'replacements' => [ 'start' => ['LOWER_NAME', 'ROUTES_LOCATION'], 'routes' => ['LOWER_NAME', 'STUDLY_NAME', 'MODULE_NAMESPACE'], + 'routes-api' => ['LOWER_NAME', 'STUDLY_NAME', 'MODULE_NAMESPACE'], 'json' => ['LOWER_NAME', 'STUDLY_NAME', 'MODULE_NAMESPACE'], 'event-service-provider' => ['LOWER_NAME', 'STUDLY_NAME', 'MODULE_NAMESPACE'], 'listener-test' => ['LOWER_NAME', 'STUDLY_NAME', 'MODULE_NAMESPACE'], @@ -30,6 +32,7 @@ return [ 'views/frontend' => ['STUDLY_NAME'], 'views/admin' => ['STUDLY_NAME'], 'controller-admin' => ['MODULE_NAMESPACE', 'STUDLY_NAME', 'CLASS_NAMESPACE', 'LOWER_NAME'], + 'controller-api' => ['MODULE_NAMESPACE', 'STUDLY_NAME', 'CLASS_NAMESPACE', 'LOWER_NAME'], 'scaffold/config' => ['STUDLY_NAME'], 'composer' => [ 'LOWER_NAME', @@ -54,8 +57,11 @@ return [ 'factory' => ['path' => 'Database/factories', 'generate' => true], 'model' => ['path' => 'Models', 'generate' => true], 'controller' => ['path' => 'Http/Controllers', 'generate' => true], + 'controller-admin' => ['path' => 'Http/Controllers/Admin', 'generate' => true], + 'controller-api' => ['path' => 'Http/Controllers/Api', 'generate' => true], 'filter' => ['path' => 'Http/Middleware', 'generate' => true], 'request' => ['path' => 'Http/Requests', 'generate' => true], + 'routes' => ['path' => 'Http/Routes', 'generate' => true], 'provider' => ['path' => 'Providers', 'generate' => true], 'assets' => ['path' => 'Resources/assets', 'generate' => true], 'lang' => ['path' => 'Resources/lang', 'generate' => true], diff --git a/modules/Sample/Http/Controllers/AdminController.php b/modules/Sample/Http/Controllers/Admin/AdminController.php similarity index 100% rename from modules/Sample/Http/Controllers/AdminController.php rename to modules/Sample/Http/Controllers/Admin/AdminController.php diff --git a/modules/Sample/Http/Controllers/Api/SampleController.php b/modules/Sample/Http/Controllers/Api/SampleController.php new file mode 100644 index 00000000..3abfd816 --- /dev/null +++ b/modules/Sample/Http/Controllers/Api/SampleController.php @@ -0,0 +1,31 @@ +message('Hello, world!'); + } + + /** + * @param Request $request + */ + public function hello(Request $request) + { + // Another way to return JSON, this for a custom response + // It's recommended to use Resources for responses from the database + return response()->json([ + 'name' => Auth::user()->name, + ]); + } + +} diff --git a/modules/Sample/Http/Routes/admin.php b/modules/Sample/Http/Routes/admin.php new file mode 100644 index 00000000..a007034e --- /dev/null +++ b/modules/Sample/Http/Routes/admin.php @@ -0,0 +1,7 @@ + []], function() { + Route::get('/', 'SampleController@index'); +}); + +/** + * This is required to have a valid API key + */ +Route::group(['middleware' => [ + 'api.auth' +]], function() { + Route::get('/hello', 'SampleController@hello'); +}); diff --git a/modules/Sample/Http/Routes/web.php b/modules/Sample/Http/Routes/web.php new file mode 100644 index 00000000..2d05fbf3 --- /dev/null +++ b/modules/Sample/Http/Routes/web.php @@ -0,0 +1,9 @@ + [ + 'role:user' # leave blank to make this public +]], function() { + # all your routes are prefixed with the above prefix + # e.g. yoursite.com/sample + Route::get('/', 'SampleController@index'); +}); diff --git a/modules/Sample/Http/routes.php b/modules/Sample/Http/routes.php deleted file mode 100644 index 30b5171e..00000000 --- a/modules/Sample/Http/routes.php +++ /dev/null @@ -1,18 +0,0 @@ - [ - 'role:admin|user' # leave blank to make this public -]], function() { - - # all your routes are prefixed with the above prefix - # e.g. yoursite.com/sample - Route::get('/', 'SampleController@index'); - - # This is the admin path. Comment this out if you don't have - # an admin panel component. - Route::group([ - 'middleware' => ['role:admin'], - ], function () { - Route::get('/admin', 'AdminController@index'); - }); -}); diff --git a/modules/Sample/Models/SampleTable.php b/modules/Sample/Models/SampleTable.php new file mode 100644 index 00000000..05768bce --- /dev/null +++ b/modules/Sample/Models/SampleTable.php @@ -0,0 +1,11 @@ + 'sample.', 'prefix' => 'sample', @@ -62,7 +65,33 @@ class SampleServiceProvider extends ServiceProvider 'middleware' => ['web'], 'namespace' => 'Modules\Sample\Http\Controllers' ], function() { - $this->loadRoutesFrom(__DIR__ . '/../Http/routes.php'); + $this->loadRoutesFrom(__DIR__ . '/../Http/Routes/web.php'); + }); + + /** + * Routes for the admin + */ + Route::group([ + 'as' => 'sample.', + 'prefix' => 'api/sample/admin', + // If you want a RESTful module, change this to 'api' + 'middleware' => ['web', 'role:admin'], + 'namespace' => 'Modules\Sample\Http\Controllers\Admin' + ], function() { + $this->loadRoutesFrom(__DIR__ . '/../Http/Routes/admin.php'); + }); + + /** + * Routes for an API + */ + Route::group([ + 'as' => 'sample.', + 'prefix' => 'api/sample', + // If you want a RESTful module, change this to 'api' + 'middleware' => ['api'], + 'namespace' => 'Modules\Sample\Http\Controllers\Api' + ], function() { + $this->loadRoutesFrom(__DIR__ . '/../Http/Routes/api.php'); }); } diff --git a/modules/Sample/composer.json b/modules/Sample/composer.json index 9b016e64..13f9b1ad 100644 --- a/modules/Sample/composer.json +++ b/modules/Sample/composer.json @@ -1,16 +1,15 @@ { - "name": "nabeel/sample", + "name": "/sample-module", "type": "laravel-module", - "license": "MIT", "description": "", "authors": [ { - "name": "Nabeel Shahzad", - "email": "gm@nabs.io" + "name": "", + "email": "" } ], "require": { - "joshbrw/laravel-module-installer": "dev-master" + "joshbrw/laravel-module-installer": "0.1.x" }, "extra": { "laravel": { diff --git a/resources/stubs/modules/composer.stub b/resources/stubs/modules/composer.stub index 9b4e90c5..f8f2c18f 100644 --- a/resources/stubs/modules/composer.stub +++ b/resources/stubs/modules/composer.stub @@ -1,12 +1,16 @@ { - "name": "$VENDOR$/$LOWER_NAME$", + "name": "VENDOR/$LOWER_NAME$-module", + "type": "laravel-module", "description": "", "authors": [ { - "name": "$AUTHOR_NAME$", - "email": "$AUTHOR_EMAIL$" + "name": "", + "email": "" } ], + "require": { + "joshbrw/laravel-module-installer": "0.1.x" + }, "extra": { "laravel": { "providers": [ diff --git a/resources/stubs/modules/controller-api.stub b/resources/stubs/modules/controller-api.stub new file mode 100644 index 00000000..72c20657 --- /dev/null +++ b/resources/stubs/modules/controller-api.stub @@ -0,0 +1,31 @@ +message('Hello, world!'); + } + + /** + * @param Request $request + */ + public function hello(Request $request) + { + // Another way to return JSON, this for a custom response + // It's recommended to use Resources for responses from the database + return response()->json([ + 'name' => Auth::user()->name, + ]); + } + +} diff --git a/resources/stubs/modules/model.stub b/resources/stubs/modules/model.stub index 9e04a66c..446bf95d 100644 --- a/resources/stubs/modules/model.stub +++ b/resources/stubs/modules/model.stub @@ -2,9 +2,10 @@ namespace $NAMESPACE$; -use Illuminate\Database\Eloquent\Model; +use App\Models\BaseModel; -class $CLASS$ extends Model +class $CLASS$ extends BaseModel { + public $table = ''; protected $fillable = $FILLABLE$; } diff --git a/resources/stubs/modules/routes-admin.stub b/resources/stubs/modules/routes-admin.stub new file mode 100644 index 00000000..a007034e --- /dev/null +++ b/resources/stubs/modules/routes-admin.stub @@ -0,0 +1,7 @@ + []], function() { + Route::get('/', '$STUDLY_NAME$Controller@index'); +}); + +/** + * This is required to have a valid API key + */ +Route::group(['middleware' => [ + 'api.auth' +]], function() { + Route::get('/hello', '$STUDLY_NAME$Controller@hello'); +}); diff --git a/resources/stubs/modules/routes.stub b/resources/stubs/modules/routes.stub index 352f275f..8a542a28 100644 --- a/resources/stubs/modules/routes.stub +++ b/resources/stubs/modules/routes.stub @@ -1,18 +1,9 @@ [ - 'role:admin|user' # leave blank to make this public + 'role:user' # leave blank to make this public ]], function() { - # all your routes are prefixed with the above prefix # e.g. yoursite.com/sample Route::get('/', '$STUDLY_NAME$Controller@index'); - - # This is the admin path. Comment this out if you don't have - # an admin panel component. - Route::group([ - 'middleware' => ['role:admin'], - ], function () { - Route::get('/admin', 'AdminController@index'); - }); }); diff --git a/resources/stubs/modules/scaffold/provider.stub b/resources/stubs/modules/scaffold/provider.stub index b4b24d01..148b40d3 100644 --- a/resources/stubs/modules/scaffold/provider.stub +++ b/resources/stubs/modules/scaffold/provider.stub @@ -55,6 +55,9 @@ class $CLASS$ extends ServiceProvider */ protected function registerRoutes() { + /** + * Routes for the frontend + */ Route::group([ 'as' => '$LOWER_NAME$.', 'prefix' => '$LOWER_NAME$', @@ -62,7 +65,33 @@ class $CLASS$ extends ServiceProvider 'middleware' => ['web'], 'namespace' => '$MODULE_NAMESPACE$\$STUDLY_NAME$\Http\Controllers' ], function() { - $this->loadRoutesFrom(__DIR__ . '/../Http/routes.php'); + $this->loadRoutesFrom(__DIR__ . '/../Http/Routes/web.php'); + }); + + /** + * Routes for the admin + */ + Route::group([ + 'as' => '$LOWER_NAME$.', + 'prefix' => 'api/$LOWER_NAME$/admin', + // If you want a RESTful module, change this to 'api' + 'middleware' => ['web', 'role:admin'], + 'namespace' => '$MODULE_NAMESPACE$\$STUDLY_NAME$\Http\Controllers\Admin' + ], function() { + $this->loadRoutesFrom(__DIR__ . '/../Http/Routes/admin.php'); + }); + + /** + * Routes for an API + */ + Route::group([ + 'as' => '$LOWER_NAME$.', + 'prefix' => 'api/$LOWER_NAME$', + // If you want a RESTful module, change this to 'api' + 'middleware' => ['api'], + 'namespace' => '$MODULE_NAMESPACE$\$STUDLY_NAME$\Http\Controllers\Api' + ], function() { + $this->loadRoutesFrom(__DIR__ . '/../Http/Routes/api.php'); }); }