Installation
composer require draw/application-bundle
Add the service provider to config/app.php:
'providers' => [
// ...
Draw\ApplicationBundle\ApplicationBundleServiceProvider::class,
],
First Use Case: Domain Logic Container
Register a domain logic handler in config/application.php (created automatically):
'handlers' => [
'user_registration' => [
'class' => App\Services\UserRegistrationHandler::class,
'method' => 'handle',
],
],
Triggering Logic
Inject the ApplicationBundle facade into a controller:
use Draw\ApplicationBundle\Facades\ApplicationBundle;
public function register(Request $request) {
$result = ApplicationBundle::run('user_registration', $request->all());
return response()->json($result);
}
Separation of Concerns
UserRegistrationHandler).Dependency Injection
Bind handlers to the container in ApplicationBundleServiceProvider:
$this->app->bind('user_registration', function ($app) {
return new UserRegistrationHandler(
$app->make(UserRepository::class),
$app->make(EmailService::class)
);
});
Middleware Integration Use the bundle to validate requests before processing:
public function handle($request, Closure $next) {
$validated = ApplicationBundle::validate('user_registration', $request->all());
if ($validated) {
return $next($request);
}
return response()->json(['error' => 'Validation failed'], 422);
}
ApplicationBundle::run('user_registration', $data);
event(new UserRegistered($data));
try-catch to log errors:
try {
ApplicationBundle::run('order_processing', $orderData);
} catch (\Exception $e) {
Log::error("Order processing failed: " . $e->getMessage());
}
Handler Registration Overrides
bind()), the last registration wins. Use extend() for merging:
$this->app->extend('user_registration', function ($handler) {
return new ExtendedUserRegistrationHandler($handler);
});
Circular Dependencies
HandlerA calls HandlerB, which calls HandlerA). Use events or queues instead.Configuration Caching
config/application.php:
php artisan config:clear
Handler Not Found
Check if the handler is properly registered in config/application.php or bound in the service provider. Enable debug mode:
ApplicationBundle::setDebug(true); // Logs unresolved handlers
Method Invocation Errors
Ensure the method key in the config matches the handler’s public method name (case-sensitive).
Custom Validators Extend validation logic by creating a validator class and binding it:
$this->app->bind('user_registration_validator', function () {
return new CustomUserValidator();
});
Pre/Post Hooks
Use Laravel’s app facade to tap into handler execution:
$this->app->afterResolving('user_registration', function ($handler) {
// Run code after handler is resolved
});
Testing Mock the bundle in tests:
$this->app->instance('user_registration', Mockery::mock(UserRegistrationHandler::class));
How can I help you explore Laravel packages today?