0
votes

Im using Laravel 5.0 and attempting to set up a command for queueing a job inside of my mailer service.

My problem is that I don't know how to inject the command dispatcher into my mailer service.

My structure:

  • Services folder
    • Mailer.php (contains abstract class Mailer)
    • UserMailer.php (contains class UserMailer which extends the Mailer class)

Things I have tried:

I've tried to use the DispatchesCommands trait within the UserMailer class and within the Mailer class. I have also tried to inject \Illuminate\Contracts\Bus\Dispatcher into the constructer of the UserMailer class.

In all three instances I get an error that "Class Services\Mailers\SendEmail Not Found"

Mailer.php:

abstract class Mailer
{
   public function emailTo($view, $mData)
   {

    Mail::queue($view, $mData, function($message) use ($mData)
    {
      //code here - not relevant 

        }
    });
 }
}

UserMailer.php:

use Illuminate\Contracts\Bus\Dispatcher as Dispatcher;

Class UserMailer extends Mailer
{

  protected $bus;

  function __construct(Dispatcher $bus)
  {
    $this->bus = $bus;

  }

  public function sendIndividualEmail($members)
  {

        // code here not relevant...

    $this->bus->dispatch(new SendEmail($members));

    return true;
  }

How do I access the command bus from within my UserMailer class? TIA

1

1 Answers

1
votes

You should import the fully qualified namespaced path for the SendEmail class, for example:

use Illuminate\Contracts\Bus\Dispatcher as Dispatcher;

use Namespace\To\SendEmail; //<-- Add this "use" statement with correct namespace

Class UserMailer extends Mailer
{
    //...
}

So, the you can use new SendEmail($members) within your class. In your code, the PHP is looking the SendEmail in Services\Mailers namespace/folder.