This package provides php attributes automatically register Laravel routes, event listeners and command bus handlers.
- PHP ^8.0
- Laravel ^8.0
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')
],
],
];
The package provides several annotations that should be put on your methods.
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');
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,
]);
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']);
composer test
The MIT License (MIT). Please see License File for more information.