THIS BRANCH IS WIP! TODO: Improve documentation
Modularize your laravel app in a package way. Inspired by
spatie/laravel-package-tools
.
This laravel modular package is built with the concept that each module is considered as a separate package. So it will allow you to structure the module folders in the same way as you build a laravel package.
$ composer require kodepandai/laravel-modular
After installation, add this configuration to your composer.json
"repositories": [
// ...
{
"type": "path",
"url": "modules/*/",
"options": {
"symlink": true
}
}
// ...
],
$ php artisan modular:make Sales
*Sales is the module name
This package DOES NOT load resources (routes, config, migration, etc)
automatically, instead you need to manually load them in
your module ServiceProvider
.
namespace Sales\Providers;
use KodePandai\Modular\Module;
use KodePandai\Modular\Providers\ModuleServiceProvider;
class SalesServiceProvider extends ModuleServiceProvider
{
public function configureModule(Module $module): void
{
$module->name('Sales')
->hasViews()
->hasMigrations()
->hasRoutes();
// $module->hasOtherThings() ...
}
}
Put your config file in the <module root>/config
folder,
then register it with:
// load all configs
$module->hasConfigs();
// load one config
$module->hasConfig('sales');
// load multiple configs
$module->hasConfigs(['sales', 'services']);
Put your view file in the <module root>/resources/views
folder,
then register it with:
// load all views
$module->hasViews();
Put your view component file in the <module root>/src/Components
folder,
then register it with:
// load one <sales::header /> component
$module->hasViewComponent('sales', \Sales\Components\Header::class);
// load multiple components
$module->hasViewComponents('sales', [
\Sales\Components\Header::class, // <sales::header />
\Sales\Components\Sidebar::class, // <sales::sidebar />
]);
Put your translation file in the <module root>/lang
or <module root>/resources/lang
folder,
then register it with:
$module->hasTranslations();
Put your migration in the <module root>/database/migrations
folder,
then register it with:
// load all migrations
$module->hasMigrations();
Note:
- For factory put in the
<module root>/database/factories
folder - For seeder put in the
<module root>/database/seeders
folder
Put your route file in the <module root>/routes
folder,
then register it with:
// load all routes
$module->hasRoutes();
// load one route
$module->hasRoute('web');
// load multiple routes
$module->hasRoutes(['web', 'api']);
Put your command file in the <module root>/src/Commands
folder,
then register it with:
// load all commands
$module->hasCommmands();
// load one command
$module->hasCommand(\Sales\Commands\CheckServer::class);
// load multiple commands
$module->hasCommands([
\Sales\Commands\CheckVersion::class,
\Sales\Commands\CheckServer::class,
]);
Put your helper function in the <module root>/src/helpers.php
file,
then register it with:
// load helper
$module->hasHelper();
Put your middleware file int the <module root>/src/Http/Middleware/
folder,
then register it with:
// load one middleware
$module->hasMiddleware('sales.admin', \Sales\Http\Middleware\EnsureAdminHaveAccessToSales::class);
// load multiple middlewares
$module->hasMiddlewares([
'sales.admin' => \Sales\Http\Middleware\EnsureAdminHaveAccessToSales::class,
'sales.whitelist' => \Sales\Http\Middleware\OnlyWhitelistIpCanAccessSales::class,
]);
Put your service provider in the <module root>/src/Providers
folder
then register it with:
// load one service Provider
$module->hasServiceProvider(\Sales\Providers\PaymentServiceProvider::class);
// load multiple service providers
$module->hasServiceProviders([
\Sales\Providers\PaymentServiceProvider::class,
\Sales\Providers\EventServiceProvider::class,
]);
Run composer test
to test.