App\User; use App\Http\Controllers\Controller; class UserController extends Controller { public function show($id) { return view('user.profile', ['user' => User::findOrFail($id)]); } }
1. Validate the input. 2. Check if logged in user can create a resource. 3. Create the resource in the database. 4. Send email to affected parties. 5. Send notification to affected parties. 6. Create / update relations / log audits. 7. ...
public $confession; public $user; public function __construct(Confession $confession, User $user) { $this->confession = $confession; $this->user = $user; } }
service can be slow (10 - 15s). • Users can’t wait that long! • Laravel provides an API to defer listeners as a queued task. • Supported providers: ◦ AWS SQS ◦ Redis
SerializesModels; // Serialize before queuing. public $confession; public $user; public function __construct(Confession $confession, User $user) { $this->confession = $confession; $this->user = $user; } }
public function __construct(JobApplication $application) { $this->application = $application; } public function toMail($notifiable) { return (new MailMessage) ->subject('You have a new job application!') ... } // Add other delivery channel functions. }
public function handle(AppliedJobApplication $event) { $application = $event->getTarget(); Notification::send( $this->getCompanyAdmins($application), new AppliedJobNotification($application) ); } }
2. Trigger the event at the controller. 3. Write each task as an event listener. 4. Attach these listeners to the event service provider. 5. Queue the listeners (optional). 6. Use notifications as an additional layer (optional).
payloads / rate throttling? Middleware • Control who can have perform an action on something? Policies • Access control based on policies? Gates • Sending to Slack / Mail / Notification Services? Notifications • Need to validate your requests? Form Requests • User can subscribe to events and listen to them? Broadcasting • Control how the model should present on response? Transformers
Laravel Docs is your best friend. ◦ Research other great third-party libraries. ▪ https://laravel-news.com/category/laravel-packages ▪ Curated list @ GitHub: https://github.com/chiraggude/awesome-laravel