Setting queues for Laravel 5




Something  I want to talk

i have already contact laravel a little time for improve my coding skill and speed, and since i learn laravel with official document, i found some problem with it.

there's no doubt laravel is powerful and beautiful but the official docs is terrible. if anyone who used to learn with official docs may find that official docs is not friendly to  newer, lack some important step(it consider that you already know) and less sample, it make me pain in learn laravel, so i prefer learn with other internet resourse.

Let's start

in this and later section i will show how to setting laravel's queues in a few step, it works well on my project!
before use queues you have to setting your ".env" file to that is know where can put my job into, you can set with some database (ex. mySql) also Redis(it's a in memory database).
see official docs what is support
i well setting with my database in this sample but consider what you need maybe Redis is more suitable
1 . in .env set this to enable queues
    QUEUE_DRIVER=database
2. make a table for queues and migrate
    $ php artisan queue:table
    $ php artisan migrate
  
now you are already to use queues!
3. make a job you want to use queues
    $ php artisan make:job SendSMS
4. go to your controller and add this namespace
    use AppJobsSendSMS;

5. take a look the file you made at AppJobsSendSMS,now do what you want in "handle" (if you want to compact variable i will write before)
6. in your controller call this to add a job to queues

    $this->dispatch(new SendSMS( ));
that's it! not to hard, isn't it?

use $ php artisan queue:listen to monitor the queue 

Compact variable

i am not going to describe anything in this section, just look how i pass it 
controller :
$this->dispatch(new SendSMS($name,$phone,$smbody));

AppJobsSendSMS :
protected $name,$phone,$smbody;
public function __construct($name,$phone,$smbody)
{
        $this->name=$name;
        $this->phone=$phone;
        $this->smbody=$smbody;
}

public function handle()

{
    //$this->name
    // $this->phone
    //$this->smbody
}

    

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料