Norway


Just released a short , explaining a topic called . I’ve noticed that it’s quite hard to understand for junior developers, but in reality it’s easy when you see the example.

So, probably the most simple example is system, where you may want to have some internal variable to add to your email/SMS notification, but you have to pass them as parameters to the class. That’s a typical example of dependency injection – see how we’re passing $user variable here.

class Hello extends Notification
{

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('Hi ' . $this->user->name . ' ('.$this->user->email.')')
                    ->line('The introduction to the notification.')
                    ->action('Notification Action', url('/'))
                    ->line('Thank you for using our application!');
    }

See video below for more explanation.

The post Dependency Injection in Laravel: Notifications Example appeared first on Laravel Daily.



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here