aidynmakhataev/laravel-attributes
Composer 安装命令:
composer require aidynmakhataev/laravel-attributes
包简介
Auto register laravel event listeners, command handlers and routes using PHP attributes
README 文档
README
This package provides php attributes automatically register Laravel routes, event listeners and command bus handlers.
Requirements
- PHP ^8.0
- Laravel ^8.0
Installation
You can install the package via composer:
composer require aidynmakhataev/laravel-attributes
You can publish the config file with:
php artisan vendor:publish --provider="AidynMakhataev\LaravelAttributes\LaravelAttributesServiceProvider" --tag="config"
This is the contents of the published config file:
return [ 'events' => [ /* * Automatic registration of listeners will only happen if this setting is `true` */ 'enabled' => true, /* * Listeners in these directories that have attributes will automatically be registered. */ 'directories' => [ base_path('app/Listeners') ] ], 'command_bus' => [ /* * Automatic registration of command handlers will only happen if this setting is `true` */ 'enabled' => true, /* * Handlers in these directories that have attributes will automatically be registered. */ 'directories' => [ base_path('app/CommandHandlers') ], ], 'routing' => [ /* * Automatic registration of routes will only happen if this setting is `true` */ 'enabled' => true, /* * Controllers in these directories that have attributes will automatically be registered. */ 'directories' => [ base_path('app/Http/Controllers') ], ], ];
Usage
The package provides several annotations that should be put on your methods.
Event Listeners
namespace App\Listeners; use AidynMakhataev\LaravelAttributes\Attributes\EventListener; use App\Events\OrderShipped; class SendShipmentNotification { #[EventListener] public function handle(OrderShipped $event) { // } }
This attribute will automatically register this listener by executing following command:
Event::listen(OrderShipped::class, 'SendShipmentNotification@handle');
Command Handlers
namespace App\CommandHandlers; use AidynMakhataev\LaravelAttributes\Attributes\CommandHandler; use App\Events\OrderShipped; class CreateOrderCommandHandler { #[CommandHandler] public function handle(CreateOrderCommand $command) { // } }
This attribute will automatically register this handler by executing following command:
Bus::map([
CreateOrderCommand::class => CreateOrderCommandHandler::class,
]);
Routing
namespace App\Http\Controllers; use AidynMakhataev\LaravelAttributes\Attributes\Route; use App\Events\OrderShipped; use Illuminate\Http\Request; class OrderController { #[Route(path: '/orders', methods: ['POST'], name: 'orders.store', middlewares: ['auth'])] public function store(Request $request) { // } }
This attribute will automatically register this route by executing following command:
Route::post('/orders', [OrderController::class, 'store'])->name('orders.store')->middlewares(['auth']);
Testing
composer test
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 11
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-09-02