vendredi 7 août 2015

Laravel 5.1 - Return Data from Jobs?

I'm just starting to use Jobs in Laravel 5.1 and am wondering if it's a good practice to return data from a Job? I haven't seen any examples of that, but here's a scenario. Lets say its an internal Messaging System between Users:

// Controller Method 
public function store(Request $request)
{
     if (!$this->messageValidator->isValid($request->all())) {
          return redirect()->back()->withInput()->withErrors($this->messageValidator->getErrors());
      }

      $this->dispatchFrom(PostMessage::class, $request, [ 'user' => Auth::user() ]);

      return redirect('messages');
}

So the Job will take the request data and the User, and will perform several tasks

// In the PostMessage Job

public function handle( // dependencies here)
 {
       // Create a new Thread

      //  Add Message to the Database

      // Store Recipients of Message in the Database 

      // Send email notifications to all involced

     return $message_id;
}

At the end of the handle() method, I returned the $message_id so I can use it for a redirect in the controller:

return view('messages.show', $message_id);

Is this an acceptable practice, or are Jobs meant to perform tasks in a more isolated manner?

Or is perhaps not a good use of Jobs?

Aucun commentaire:

Enregistrer un commentaire