Brief introduction
We have studied Events and Listeners, and this time we are studying Jobs.
The creation of jobs
Run the command line PHP artisan make:job compilereports Create a job class, the job class structure is simple, in general, will only include a queue to invoke this task handle method, the specific code is as follows:
Class Compilereports extends job implements selfhandling{ /** * Create a new job instance. * * @return void * /Public Function __construct () { // } /** * Execute the job. * * @return void * /Public Function handle () { //
With PHP artisan help Make:job to observe the relevant parameters, the implementation of PHP artisan Make:job compilereports--queued, compared to the previous class difference to realize the Shouldqueue interface, which means Can be executed in queue order.
Increase Routing and Controller
Added in default route routes.php:
Add a test method by executing the command line PHP artisan make:controller reportscontroller--plain, creating the Reportscontroller controller:
Class Reportscontroller extends controller{ //Public Function index () { $job = new Compilereports (); c4/> $this->dispatch ($job); return ' done ';
The dispatch ($job) method, which is used dispatchesjobs, is defined by default in the Controller parent class for Reportscontroller controllers to dispatch tasks. At this point we visit the page effects such as:
Parameter handling in Job
Overwrite the compilereports.php code, and the constructor accepts $reportId parameters:
Class Compilereportsextends jobimplements selfhandling, shouldqueue{ protected $reportId; /** * Create a new job instance. * * @return void * /Public Function __construct ($reportId) { // $this->reportid = $ ReportID; } /** * Execute the job. * * @return void * /Public Function handle () { // var_dump (' compiling the reports with The id '. $this->reportid ' within the Job class. ');
The Reportscontroller controller accepts the parameters in the request and forwards it to job processing:
Class Reportscontroller extends controller{public function Index (Request $request) { $job = new Compilereports ($request->input (' ReportID ')); $this->dispatch ($job); return ' done ';
The access page works as follows:
Multi-parameter processing in job
In Reportscontroller, call the Dispatchfrom method instead:
Class Reportscontroller extends controller{ //Public function Index (Request $request) { //$job = new Compilereports ($request->input (' ReportID ')); $this->dispatchfrom (Compilereports::class, $request); $this->dispatchfrom (' App\jobs\compilereports ', $request); return ' done ';
Dispatch and Dispatchfrom are defined in Dispatchesjobs, as follows:
Trait dispatchesjobs{/** * Dispatch a job to its appropriate handler. * * @param mixed $job * @return Mixed */protected function Dispatch ($job) {return app (' Illumi Nate\contracts\bus\dispatcher ')->dispatch ($job); }/** * Marshal a job and dispatch it to its appropriate handler. * * @param mixed $job * @param array $array * @return Mixed */protected function Dispatchfromarray ($job, array $array) {return app (' Illuminate\contracts\bus\dispatcher ')->dispatchfromarray ($job, $array); }/** * Marshal a job and dispatch it to its appropriate handler. * Organize parameter list, pass to Job * @param mixed $job * @param \arrayaccess $source * @param array $extras * @return m ixed */protected function Dispatchfrom ($job, arrayaccess $source, $extras = []) {return app (' Illuminate \contracts\bus\dispatcher ')->dispatchfrom ($job, $source, $extras); }}
To overwrite an incoming parameter in the Compilereports class:
Class Compilereportsextends jobimplements selfhandling, shouldqueue{ protected $reportId; protected $type; /** * Create a new job instance. * * @return void */public function __construct ($reportId, $type) { // $this->reportid = $reportId; $this->type = $type; } /** * Execute the job. * * @return void * /Public Function handle () { // var_dump (' compiling the '. $this Type. ' Reports with the ID '. $this->reportid ' within the Job class. ');
Access effects such as:
After you add the Dispatchesjobs reference in the job, you can continue to dispatch the job:
/*** Execute the job.** @return void*/public function handle () { //var_dump (' compiling the '. $this->type. ' Report s with the id '. $this->reportid ' within the Job class. '); Var_dump (sprintf (' compiling the%s report with the ID%s within the Job class ', $this->type, $this->reportid)); After adding the Dispatchesjobs reference, you can continue to assign the job if (true) { $this->dispatch (new Dosomethingelse);
Access effects such as: