I am trying to run code after returning a HTTP response. I know that Laravel 5 has support for queues, but I still find them confusing. I am trying to run code after the user has registered which requires the user's password and username. This answer seems interesting, but is not directly applicable to Laravel.
I know this sounds lazy and all, but I really don't understand the documentation.
Setting up queues requires, as the very first step, to choose what driver you'll be using. Because it's the quickest to get running, I'll explain how to begin with the database
driver, as it doesn't require any other services to be installed on the server (as it's the case for beanstalkd
for example). Here's how to get this set up:
1. Set the QUEUE_DRIVER
in your .env
file:
QUEUE_DRIVER=database
2. Run this command to generate the migration file for the jobs
table, that will be used to store job information:
php artisan queue:table
3. Now run the migration to create the table:
php artisan migrate
A jobs
table was created that will store data when jobs are pushed on the queue.
You can push both commands and clojures onto queues. For the sake of brevity, I'll show an example of how to push a closure onto a queue:
$username = Request::input('username');
$password = Request::input('password');
// Do your registration stuff
// Push a job onto the queue
\Queue::push(function($job) use ($username, $password)
{
// Do the stuff you need here with $username and $password
// Delete the job from the queue
$job->delete();
});
The final step to making this work is to run the queue listener. Jobs will not be processed automatically unless the queue listener is running. So run this command:
php artisan queue:listen
There are further steps you can take, such as setting up Supervisor to monitor and restart the queue listener should it crash, but this should be enough to get you started.
Generally we pass data on the queue like this -
On the controller we have written -
$this->dispatch(new videoToAudioConvert($video_id))
On the job section you have to write like this -
protected $video_id
public function __contructor($video_id){
$this->video_id = $video_id
}
public function handle(){
$this->video_id
}
You can get more idea how to create jobs in queue and how to pass variable from here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With