Updates for addon/modules to make it easier to create APIs

This commit is contained in:
Nabeel Shahzad
2018-02-08 19:09:36 -06:00
parent b4ea82f327
commit bcc6be0d29
18 changed files with 216 additions and 44 deletions

View File

@@ -0,0 +1,31 @@
<?php
namespace Modules\Sample\Http\Controllers\Api;
use App\Http\Controllers\Api\RestController;
use Illuminate\Http\Request;
class SampleController extends RestController
{
/**
* Just send out a message
* @param Request $request
*/
public function index(Request $request)
{
return $this->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,
]);
}
}

View File

@@ -0,0 +1,7 @@
<?php
# This is the admin path. Comment this out if you don't have
# an admin panel component.
Route::group([], function () {
Route::get('/', 'AdminController@index');
});

View File

@@ -0,0 +1,17 @@
<?php
/**
* This is publicly accessible
*/
Route::group(['middleware' => []], 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');
});

View File

@@ -0,0 +1,9 @@
<?php
Route::group(['middleware' => [
'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');
});

View File

@@ -1,18 +0,0 @@
<?php
Route::group(['middleware' => [
'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');
});
});

View File

@@ -0,0 +1,11 @@
<?php
namespace Modules\Sample\Models;
use App\Models\BaseModel;
class SampleTable extends BaseModel
{
public $table = '';
protected $fillable = [];
}

View File

@@ -55,6 +55,9 @@ class SampleServiceProvider extends ServiceProvider
*/
protected function registerRoutes()
{
/**
* Routes for the frontend
*/
Route::group([
'as' => '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');
});
}

View File

@@ -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": {