- Can I use Broadway in Laravel for a simple CRUD app, or is it only for complex event-driven systems?
- Broadway is designed for event-driven architectures like CQRS/Event Sourcing, which may be overkill for basic CRUD apps. It excels in domains requiring audit trails, workflows, or high-coherence data (e.g., financial systems). For CRUD, Laravel’s Eloquent or API resources might suffice, but Broadway’s modularity lets you adopt components like the EventStore selectively if needed.
- How do I install Broadway in Laravel and integrate it with the service container?
- Install via Composer: `composer require broadway/broadway`. Use the `nwidart/laravel-broadway` bridge package to register Broadway components as Laravel services. Configure the service provider in `config/app.php` and bind your aggregates, command handlers, and event stores to the container. The bridge handles Laravel’s dependency injection seamlessly.
- What Laravel versions does Broadway support, and are there PHP version requirements?
- Broadway requires PHP 7.4+ and is compatible with Laravel 8+. The core package aligns with Laravel’s service container and middleware patterns, but always check the latest `nwidart/laravel-broadway` release for version-specific notes. For older Laravel versions (e.g., 7), you may need to pin dependencies or use legacy branches.
- How do I set up an EventStore in Laravel using Broadway, and which backends are supported?
- Use the `broadway/event-store-dbal` package for Doctrine DBAL-backed storage, which integrates with Laravel’s database connections. Configure it in your service provider to point to your Laravel database. Other backends like MongoDB or in-memory stores are supported but require additional packages. For high throughput, consider snapshotting to reduce event replay overhead.
- Can I replace Laravel’s native request handling with Broadway’s CommandBus for API endpoints?
- Yes, you can dispatch commands from HTTP requests using Broadway’s `CommandBus`. Create custom middleware (e.g., `HandleCommandMiddleware`) to resolve commands from routes and inject the bus. For example, map a route to a command handler like `Route::post('/orders', OrderCommandHandler::class)`. This works alongside Laravel’s routing but enforces CQRS separation.
- How do I test event-sourced aggregates in Laravel with Broadway’s testing helpers?
- Broadway provides scenario-based testing for aggregates. Use `ScenarioTester` to replay events and verify state transitions. For Laravel, combine this with PHPUnit or Pest to mock dependencies like the EventStore. Example: `$scenarioTester->given([new OrderCreatedEvent()])->when(new ApplyPaymentCommand())->then($aggregate->getId()->equals($expectedId))`. This replaces traditional unit tests with behavior-driven scenarios.
- What are the performance implications of using Broadway’s EventStore in production?
- DBAL-backed EventStores may introduce latency for high-throughput systems due to event replay. Mitigate this with snapshotting (storing aggregate state periodically) and async projections via Laravel Queues. For read-heavy workloads, offload projections to Elasticsearch or MongoDB. Benchmark with your expected event volume—Broadway’s modularity lets you optimize critical paths (e.g., in-memory stores for testing).
- How do I handle authentication/authorization for commands in Laravel with Broadway?
- Broadway doesn’t include built-in auth, but you can integrate Laravel’s middleware or gates. For example, wrap command handlers in middleware to check permissions: `public function handle(CreateOrderCommand $command, Closure $next) { if (!auth()->can('create_orders')) throw new UnauthorizedException(); return $next($command); }`. Alternatively, use Laravel Policies or custom guards within command handlers.
- Are there alternatives to Broadway for CQRS/Event Sourcing in Laravel, and how do they compare?
- Alternatives include **Axon Framework** (Java/PHP port, more opinionated) and **Ncqrs** (lightweight but less Laravel-integrated). Broadway stands out for its loose coupling—you can use its EventStore without full CQRS. For Laravel-specific needs, **Laravel Event Sourcing** packages like `spatie/laravel-event-sourcing` are simpler but lack Broadway’s projection and saga support. Choose based on your need for modularity vs. ease of setup.
- How do I migrate an existing Laravel app to Broadway incrementally, without a big-bang rewrite?
- Start with a proof of concept: pick one aggregate (e.g., `Order`) and implement it with Broadway’s EventStore and commands. Use in-memory read models for testing. Gradually replace Laravel’s request handling with Broadway’s `CommandBus` for critical paths. Phase 2: Add projections (e.g., Elasticsearch) and snapshotting. Use Laravel Queues for async projections to avoid blocking requests. Document the migration path per domain.