erlandmuchasaj / laravel-modules
Laravel模块管理。
Requires
- php: ^8.0|^8.1|^8.2
- ext-json: *
Requires (Dev)
- laravel/framework: ^8.0|^9.0|^10.0|^11.0
- orchestra/testbench: ^8.0
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.0
README
一个非常简单的自动注入Laravel模块生成器,使用Laravel最佳实践和文件夹结构。如果你喜欢Laravel,你一定会喜欢laravel modules,感觉就像在家一样。
它遵循与Laravel项目相同的文件夹结构,唯一不同的是,你使用的是src/而不是app/,其余的都相同。
安装
你可以通过composer安装此包
composer require erlandmuchasaj/laravel-modules
用法
基本上,你需要做的就是生成一个新的模块。
php artisan module:make <ModuleName>
当你生成一个新的模块时,它将自动注册自己,通过将模块添加到composer.json
文件中所需列表中
"require": { "modules/{{module_name}}": "^1.0" },
它还会自动添加存储库
"repositories": [ { "type": "path", "url": "./modules/*" } ]
并且它会在后台运行composer update
以发现新生成的模块,然后你就可以开始了。
创建新模块
在这里,所有操作都在模块内部完成。基本上,一个模块是另一个Laravel应用内部的一个Laravel应用,包括所有组件、视图、路由、中间件、事件、数据库、种子、工厂等等。
php artisan module:make <ModuleName>
- 模块是自动发现的,因此无需将其添加到app/config.php提供者列表中。
- 然后运行
composer update
,你就可以开始了(即使添加模块时也会自动运行via php artisan module:make <ModuleName>
)。
Artisan命令
php artisan module:list Show list of all modules created. php artisan module:make Create blueprint for a new module php artisan module:make-cast Create a new custom Eloquent cast class php artisan module:make-channel Create a new channel class php artisan module:make-command Create a new Artisan command php artisan module:make-component Create a new component-class for the specified module. php artisan module:make-controller Create a new controller class php artisan module:make-event Create a new event class php artisan module:make-exception Create a new custom exception class php artisan module:make-factory Create a new model factory php artisan module:make-job Create a new job class php artisan module:make-listener Create a new event listener class php artisan module:make-mail Create a new email class php artisan module:make-middleware Create a new middleware class php artisan module:make-migration Create a new migration file for module. php artisan module:make-model Create a new Eloquent model class php artisan module:make-notification Create a new notification class php artisan module:make-observer Create a new observer class php artisan module:make-policy Create a new policy class php artisan module:make-provider Create a new service provider class php artisan module:make-request Create a new form request class php artisan module:make-resource Create a new resource php artisan module:make-rule Create a new validation rule php artisan module:make-scope Create a new scope class php artisan module:make-seeder Create a new seeder class php artisan module:make-test Create a new test class php artisan module:make-trait Make trait
如果你想要注册事件、策略、观察者、中间件、命令、提供者等,你可以在./modules/ModuleName/Providers/EventServiceProvider
和./modules/ModuleName/Providers/AppServiceProvider
中进行。
示例
注册新的提供者。前往./modules/ModuleName/Providers/AppServiceProvider
,并将提供者添加到$providers
数组中,如下所示
/** * Get the services provided by the provider. * * @var array<int, class-string> */ protected array $providers = [ ... NewServiceProvider::class, // <= new provider added here ];
订阅和/或监听事件。前往./modules/ModuleName/Providers/EventServiceProvider
,并在$subscribe
数组中添加事件订阅者监听器
/** * Class event subscribers. * * @var array<int, class-string> */ protected $subscribe = [ AnnouncementEventListener::class ];
或者你就像在Laravel中通常那样监听事件
/** * The event listener mappings for the application. * * @var array<class-string, array<int, class-string>> */ protected $listen = [ Registered::class => [ SendEmailVerificationNotification::class, ], ];
添加新的策略。前往./modules/ModuleName/Providers/AppServiceProvider
,并将Model
和相应的Policy
添加到$policies
数组中,如下所示
/** * The policy mappings for the application. * Announcement::class is the model <== * * @var array<class-string, class-string> */ protected array $policies = [ Announcement::class => AnnouncementPolicy::class, ];
添加新的观察者。前往./modules/ModuleName/Providers/AppServiceProvider
,并将Model
和相应的Observer
添加到$observers
数组中,如下所示
/** * The policy mappings for the application. * Announcement::class is the model <== * * @var array<class-string, class-string> */ protected array $policies = [ Announcement::class => AnnouncementObserver::class, ];
添加新的命令。前往./modules/ModuleName/Providers/AppServiceProvider
,并将命令名称添加到$commands
数组中,如下所示
/** * The available command shortname. * * @var array<int, class-string> */ protected array $commands = [ AppVersion::class, SendAnnouncementNotifications::class ];
中间件。你可以添加三种类型的中间件。全局、分组和路由。
/** * The application's global middleware stack. * * @var array<int, class-string> */ protected array $middleware = [ ... AddXHeader::class, // <== Add global middleware here ]; /** * The application's route middleware groups. * * @var array<string, array<int, class-string>> */ protected array $middlewareGroups = [ 'web' => [ RememberLocale::class, ], 'api' => [ IdempotencyMiddleware::class, ], // <== Add grouped middleware here. you can add to existing group or create new groups. ]; /** * The application's route middleware. * These middleware may be assigned to group or used individually. * * @var array<string, class-string> */ protected array $routeMiddleware = [ ... 'ip_whitelist' => IpWhitelist::class, // <== Add route middleware here ];
支持我
我投入了大量时间和资源来创建一流的开放源代码包。
如果你觉得这个包很有帮助,你可以通过点击下面的按钮并捐赠一些金额来支持我,帮助我经常在这些项目上工作。
变更日志
有关最近更改的更多信息,请参阅CHANGELOG。
贡献
请参阅CONTRIBUTING以获取详细信息。
安全漏洞
请参阅SECURITY以获取详细信息。
致谢
许可协议
MIT许可协议(MIT)。请参阅许可文件以获取更多信息。