Update module generation #714 (#716)

* Update module generation #714

* Fix method signatures

* Fix paths within stubs, use single provider.stub file

* Add separate index controller

* Update module generation #714

* Fix method signatures

* Fix paths within stubs, use single provider.stub file

* Update module generation

Disable/lower the cache time as found as a workaround in https://github.com/nWidart/laravel-modules/issues/995

* Update editorconfig for line endings

* Formatting

* Formatting
This commit is contained in:
Nabeel S
2020-09-20 19:10:52 -04:00
committed by GitHub
parent 435fa32663
commit 77fe6679ce
20 changed files with 414 additions and 248 deletions

View File

@@ -14,8 +14,9 @@
"extra": {
"laravel": {
"providers": [
"$MODULE_NAMESPACE$\\$STUDLY_NAME$\\Providers\\$STUDLY_NAME$ServiceProvider",
"$MODULE_NAMESPACE$\\$STUDLY_NAME$\\Providers\\EventServiceProvider"
"$MODULE_NAMESPACE$\\$STUDLY_NAME$\\Providers\\AppServiceProvider",
"$MODULE_NAMESPACE$\\$STUDLY_NAME$\\Providers\\EventServiceProvider",
"$MODULE_NAMESPACE$\\$STUDLY_NAME$\\Providers\\RouteServiceProvider"
],
"aliases": {

View File

@@ -6,52 +6,71 @@ use App\Contracts\Controller;
use Illuminate\Http\Request;
/**
* Class AdminController
* @package $MODULE_NAMESPACE$\$STUDLY_NAME$\Http\Controllers\Admin
* Admin controller
*/
class AdminController extends Controller
{
/**
* Display a listing of the resource.
*
* @param Request $request
*
* @return mixed
*/
public function index()
public function index(Request $request)
{
return view('$LOWER_NAME$::admin.index');
}
/**
* Show the form for creating a new resource.
*
* @param Request $request
*
* @return mixed
*/
public function create()
public function create(Request $request)
{
return view('$LOWER_NAME$::admin.create');
}
/**
* Store a newly created resource in storage.
*
* @param Request $request
*/
public function store(Request $request)
{
}
/**
* Show the specified resource.
*/
public function show()
{
return view('$LOWER_NAME$::admin.show');
}
/**
* Show the form for editing the specified resource.
*
* @param Request $request
*
* @return mixed
*/
public function edit()
public function edit(Request $request)
{
return view('$LOWER_NAME$::admin.edit');
}
/**
* Show the specified resource.
*
* @param Request $request
*
* @return mixed
*/
public function show(Request $request)
{
return view('$LOWER_NAME$::admin.show');
}
/**
* Update the specified resource in storage.
*
* @param Request $request
*/
public function update(Request $request)
{
@@ -59,8 +78,10 @@ class AdminController extends Controller
/**
* Remove the specified resource from storage.
*
* @param Request $request
*/
public function destroy()
public function destroy(Request $request)
{
}
}

View File

@@ -13,7 +13,9 @@ class ApiController extends Controller
{
/**
* Just send out a message
*
* @param Request $request
*
* @return mixed
*/
public function index(Request $request)
@@ -23,7 +25,9 @@ class ApiController extends Controller
/**
* Handles /hello
*
* @param Request $request
*
* @return mixed
*/
public function hello(Request $request)

View File

@@ -0,0 +1,90 @@
<?php
namespace $MODULE_NAMESPACE$\$STUDLY_NAME$\Http\Controllers\Frontend;
use App\Contracts\Controller;
use Illuminate\Http\Request;
/**
* Class $CLASS$
* @package $CLASS_NAMESPACE$
*/
class IndexController extends Controller
{
/**
* Display a listing of the resource.
*
* @param Request $request
*
* @return mixed
*/
public function index(Request $request)
{
return view('$LOWER_NAME$::index');
}
/**
* Show the form for creating a new resource.
*
* @param Request $request
*
* @return mixed
*/
public function create(Request $request)
{
return view('$LOWER_NAME$::create');
}
/**
* Store a newly created resource in storage.
*
* @param Request $request
*
* @return mixed
*/
public function store(Request $request)
{
}
/**
* Show the specified resource.
*
* @param Request $request
*
* @return mixed
*/
public function show(Request $request)
{
return view('$LOWER_NAME$::show');
}
/**
* Show the form for editing the specified resource.
*
* @param Request $request
*
* @return mixed
*/
public function edit(Request $request)
{
return view('$LOWER_NAME$::edit');
}
/**
* Update the specified resource in storage.
*
* @param Request $request
*/
public function update(Request $request)
{
}
/**
* Remove the specified resource from storage.
*
* @param Request $request
*/
public function destroy(Request $request)
{
}
}

View File

@@ -1,6 +1,6 @@
<?php
namespace $CLASS_NAMESPACE$;
namespace $MODULE_NAMESPACE$\$STUDLY_NAME$\Http\Controllers\Frontend;
use App\Contracts\Controller;
use Illuminate\Http\Request;
@@ -13,23 +13,34 @@ class $CLASS$ extends Controller
{
/**
* Display a listing of the resource.
*
* @param Request $request
*
* @return mixed
*/
public function index()
public function index(Request $request)
{
return view('$LOWER_NAME$::index');
}
/**
* Show the form for creating a new resource.
*
* @param Request $request
*
* @return mixed
*/
public function create()
public function create(Request $request)
{
return view('$LOWER_NAME$::create');
}
/**
* Store a newly created resource in storage.
* @param Request $request
*
* @param Request $request
*
* @return mixed
*/
public function store(Request $request)
{
@@ -37,22 +48,32 @@ class $CLASS$ extends Controller
/**
* Show the specified resource.
*
* @param Request $request
*
* @return mixed
*/
public function show()
public function show(Request $request)
{
return view('$LOWER_NAME$::show');
}
/**
* Show the form for editing the specified resource.
*
* @param Request $request
*
* @return mixed
*/
public function edit()
public function edit(Request $request)
{
return view('$LOWER_NAME$::edit');
}
/**
* Update the specified resource in storage.
*
* @param Request $request
*/
public function update(Request $request)
{
@@ -60,8 +81,10 @@ class $CLASS$ extends Controller
/**
* Remove the specified resource from storage.
*
* @param Request $request
*/
public function destroy()
public function destroy(Request $request)
{
}
}

View File

@@ -6,8 +6,9 @@
"active": 1,
"order": 0,
"providers": [
"$MODULE_NAMESPACE$\\$STUDLY_NAME$\\Providers\\$STUDLY_NAME$ServiceProvider",
"$MODULE_NAMESPACE$\\$STUDLY_NAME$\\Providers\\EventServiceProvider"
"$MODULE_NAMESPACE$\\$STUDLY_NAME$\\Providers\\AppServiceProvider",
"$MODULE_NAMESPACE$\\$STUDLY_NAME$\\Providers\\EventServiceProvider",
"$MODULE_NAMESPACE$\\$STUDLY_NAME$\\Providers\\RouteServiceProvider"
],
"aliases": {},
"files": [],

View File

@@ -3,12 +3,17 @@
namespace $MODULE_NAMESPACE$\$STUDLY_NAME$\Listeners;
use App\Events\TestEvent;
use Log;
use Illuminate\Support\Facades\Log;
/**
* A sample event listener
*/
class TestEventListener
{
/**
* Handle the event.
*
* @param \App\Events\TestEvent $event
*/
public function handle(TestEvent $event) {
Log::info('Received event', [$event]);

View File

@@ -1,26 +1,37 @@
<?php
namespace $NAMESPACE$;
namespace $MODULE_NAMESPACE$\$STUDLY_NAME$\Providers;
use Illuminate\Support\ServiceProvider;
use App\Contracts\Modules\ServiceProvider;
/**
* Class $CLASS$
* @package $NAMESPACE$
*/
class $CLASS$ extends ServiceProvider
class AppServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
private $moduleSvc;
protected $defer = false;
/**
* Boot the application events.
*/
public function boot(): void
{
$this->moduleSvc = app('App\Services\ModuleService');
$this->registerTranslations();
$this->registerConfig();
$this->registerViews();
$this->registerLinks();
// Uncomment this if you have migrations
// $this->loadMigrationsFrom(__DIR__ . '/../$MIGRATIONS_PATH$');
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
@@ -28,12 +39,55 @@ class $CLASS$ extends ServiceProvider
}
/**
* Get the services provided by the provider.
*
* @return array
* Add module links here
*/
public function provides()
public function registerLinks(): void
{
return [];
// Show this link if logged in
// $this->moduleSvc->addFrontendLink('$STUDLY_NAME$', '/$LOWER_NAME$', '', $logged_in=true);
// Admin links:
$this->moduleSvc->addAdminLink('$STUDLY_NAME$', '/admin/$LOWER_NAME$');
}
/**
* Register config.
*/
protected function registerConfig()
{
$this->publishes([
__DIR__.'/../Config/config.php' => config_path('$LOWER_NAME$.php'),
], '$LOWER_NAME$');
$this->mergeConfigFrom(__DIR__.'/../Config/config.php', '$LOWER_NAME$');
}
/**
* Register views.
*/
public function registerViews()
{
$viewPath = resource_path('views/modules/$LOWER_NAME$');
$sourcePath = __DIR__.'/../Resources/views';
$this->publishes([$sourcePath => $viewPath],'views');
$this->loadViewsFrom(array_merge(array_map(function ($path) {
return $path . '/modules/$LOWER_NAME$';
}, \Config::get('view.paths')), [$sourcePath]), '$LOWER_NAME$');
}
/**
* Register translations.
*/
public function registerTranslations()
{
$langPath = resource_path('lang/modules/$LOWER_NAME$');
if (is_dir($langPath)) {
$this->loadTranslationsFrom($langPath, '$LOWER_NAME$');
} else {
$this->loadTranslationsFrom(__DIR__ .'/../Resources/lang', '$LOWER_NAME$');
}
}
}

View File

@@ -4,19 +4,19 @@ namespace $NAMESPACE$;
use Illuminate\Routing\Router;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;
/**
* Class $CLASS$
* @package $NAMESPACE$
* Register the routes required for your module here
*/
class $CLASS$ extends ServiceProvider
class RouteServiceProvider extends ServiceProvider
{
/**
* The root namespace to assume when generating URLs to actions.
*
* @var string
*/
protected $rootUrlNamespace = '$MODULE_NAMESPACE$\$MODULE$\Http\Controllers';
protected $namespace = '$MODULE_NAMESPACE$\$MODULE$\$CONTROLLER_NAMESPACE$';
/**
* Called before routes are registered.
@@ -34,12 +34,62 @@ class $CLASS$ extends ServiceProvider
/**
* Define the routes for the application.
*
* @param \Illuminate\Routing\Router $router
*
* @return void
*/
public function map(Router $router)
{
// if (!app()->routesAreCached()) {
// require __DIR__ . '$ROUTES_PATH$';
// }
$this->registerWebRoutes();
$this->registerAdminRoutes();
$this->registerApiRoutes();
}
/**
*
*/
protected function registerWebRoutes(): void
{
$config = [
'as' => '$LOWER_NAME$.',
'prefix' => '$LOWER_NAME$',
'namespace' => $this->namespace.'\Frontend',
'middleware' => ['web'],
];
Route::group($config, function() {
$this->loadRoutesFrom(__DIR__.'/../Http/Routes/web.php');
});
}
protected function registerAdminRoutes(): void
{
$config = [
'as' => 'admin.$LOWER_NAME$.',
'prefix' => 'admin/$LOWER_NAME$',
'namespace' => $this->namespace.'\Admin',
'middleware' => ['web', 'role:admin'],
];
Route::group($config, function() {
$this->loadRoutesFrom(__DIR__.'/../Http/Routes/web.php');
});
}
/**
* Register any API routes your module has. Remove this if you aren't using any
*/
protected function registerApiRoutes(): void
{
$config = [
'as' => 'admin.$LOWER_NAME$.',
'prefix' => 'admin/$LOWER_NAME$',
'namespace' => $this->namespace.'\Api',
'middleware' => ['api'],
];
Route::group($config, function() {
$this->loadRoutesFrom(__DIR__.'/../Http/Routes/api.php');
});
}
}

View File

@@ -1,6 +1,4 @@
<?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');
});
Route::get('/', 'AdminController@index');

View File

@@ -1,9 +1,11 @@
<?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('/', '$STUDLY_NAME$Controller@index');
});
Route::get('/', '$STUDLY_NAME$Controller@index');
/*
* To register a route that needs to be authentication, wrap it in a
* Route::group() with the auth middleware
*/
// Route::group(['middleware' => 'auth'], function() {
// Route::get('/', '$STUDLY_NAME$Controller@index');
// })

View File

@@ -1,152 +0,0 @@
<?php
namespace $NAMESPACE$;
use Illuminate\Support\ServiceProvider;
use Route;
/**
* Class $CLASS$
* @package $NAMESPACE$
*/
class $CLASS$ extends ServiceProvider
{
private $moduleSvc;
protected $defer = false;
/**
* Boot the application events.
*/
public function boot()
{
$this->moduleSvc = app('App\Services\ModuleService');
$this->registerRoutes();
$this->registerTranslations();
$this->registerConfig();
$this->registerViews();
$this->registerLinks();
$this->loadMigrationsFrom(__DIR__ . '/../$MIGRATIONS_PATH$');
}
/**
* Register the service provider.
*/
public function register()
{
//
}
/**
* Add module links here
*/
public function registerLinks()
{
// Show this link if logged in
// $this->moduleSvc->addFrontendLink('$STUDLY_NAME$', '/$LOWER_NAME$', '', $logged_in=true);
// Admin links:
$this->moduleSvc->addAdminLink('$STUDLY_NAME$', '/admin/$LOWER_NAME$');
}
/**
* Register the routes
*/
protected function registerRoutes()
{
/**
* Routes for the frontend
*/
Route::group([
'as' => '$LOWER_NAME$.',
'prefix' => '$LOWER_NAME$',
// If you want a RESTful module, change this to 'api'
'middleware' => ['web'],
'namespace' => '$MODULE_NAMESPACE$\$STUDLY_NAME$\Http\Controllers'
], function() {
$this->loadRoutesFrom(__DIR__ . '/../Http/Routes/web.php');
});
/**
* Routes for the admin
*/
Route::group([
'as' => '$LOWER_NAME$.',
'prefix' => 'admin/$LOWER_NAME$',
// 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');
});
}
/**
* Register config.
*/
protected function registerConfig()
{
$this->publishes([
__DIR__.'/../$PATH_CONFIG$/config.php' => config_path('$LOWER_NAME$.php'),
], '$LOWER_NAME$');
$this->mergeConfigFrom(
__DIR__.'/../$PATH_CONFIG$/config.php', '$LOWER_NAME$'
);
}
/**
* Register views.
*/
public function registerViews()
{
$viewPath = resource_path('views/modules/$LOWER_NAME$');
$sourcePath = __DIR__.'/../$PATH_VIEWS$';
$this->publishes([
$sourcePath => $viewPath
],'views');
$this->loadViewsFrom(array_merge(array_map(function ($path) {
return $path . '/modules/$LOWER_NAME$';
}, \Config::get('view.paths')), [$sourcePath]), '$LOWER_NAME$');
}
/**
* Register translations.
*/
public function registerTranslations()
{
$langPath = resource_path('lang/modules/$LOWER_NAME$');
if (is_dir($langPath)) {
$this->loadTranslationsFrom($langPath, '$LOWER_NAME$');
} else {
$this->loadTranslationsFrom(__DIR__ .'/../$PATH_LANG$', '$LOWER_NAME$');
}
}
/**
* Get the services provided by the provider.
*/
public function provides()
{
return [];
}
}