
Events & Listeners
Event creation, listener registration, dispatching events, event subscribers, queued listeners, broadcast events
1What is an Event in Laravel?
What is an Event in Laravel?
Antwort
An Event is a class that represents a significant action or occurrence that happened in the application. It serves as a data-carrying message that can be dispatched and listened to by multiple listeners in a decoupled manner. Events allow respecting the Open/Closed principle by adding behaviors without modifying existing code.
2How to create an Event and its associated Listener with Artisan?
How to create an Event and its associated Listener with Artisan?
Antwort
The command 'php artisan make:event' creates an Event, and 'php artisan make:listener' creates a Listener. By adding the --event=EventName option, the Listener is automatically typed to receive this Event. It's also possible to define the mapping in EventServiceProvider then run 'php artisan event:generate' to automatically generate all missing Events and Listeners.
3Where to register the mapping between Events and Listeners in Laravel?
Where to register the mapping between Events and Listeners in Laravel?
Antwort
The Events-Listeners mapping is done in the $listen property of EventServiceProvider (App\Providers\EventServiceProvider). Each key is the fully qualified Event class name, and the value is an array of Listeners. Since Laravel 11, it's also possible to use auto-discovery with PHP 8 attributes #[ListensTo] to avoid manual registration.
How to dispatch (trigger) an Event in Laravel?
What is the basic structure of a Listener in Laravel?
+15 Interview-Fragen
Weitere Laravel-Interviewthemen
PHP Basics
PHP OOP Essentials
Composer & Autoloading
Laravel Fundamentals
Laravel Routing
Blade Templates
Request & Response
Eloquent ORM Basics
Eloquent Relationships
Migrations & Schema Builder
Validation & Forms
Authentication
Authorization & Policies
API Resources & Authentication
Middleware
Service Container & DI
Queues & Jobs
Notifications & Mail
File Storage
Testing & PHPUnit
Caching
Livewire & Inertia
Eloquent Advanced
Repository Pattern
Laravel Packages
Performance Optimization
Security Best Practices
Laravel Octane
Laravel Distributed Systems
Observability & Monitoring
Deployment & DevOps
Meistere Laravel für dein nächstes Interview
Zugang zu allen Fragen, Flashcards, technischen Tests, Code-Review-Übungen und Interview-Simulatoren.
Kostenlos starten