php - Scheduling App/Commands in Laravel 5 -
here sendbirthdayemailcommand
command created under /app/commands/
directory
class sendbirthdayemailcommand extends command implements shouldbequeued { use interactswithqueue, serializesmodels; public function __construct() { } }
with handler
class sendbirthdayemailcommandhandler
in /app/handlers/
diretory
class sendbirthdayemailcommandhandler { public function __construct() { // } public function handle(sendbirthdayemailcommand $command) { // $reminders = \app\reminder::all()->where('reminder_status','=','scheduled') ->where('reminder_set','=','birthday') ->where('reminder_type','=','email') ->where('reminder_time','<=', \carbon\carbon::now('asia/kolkata')); foreach ($reminders $reminder) { $this->sendemailnow($reminder); } } public function sendemailnow($reminder_record) { $reminderdata = $reminder_record; $from = $reminderdata->reminder_from; $to = $reminderdata->reminder_to; $msgstring = $reminderdata->reminder_msg; \illuminate\support\facades\mail::queueon( 'birthday_email', ['html' => 'emails.bdaymailhtml'], $msgstring, function($message){ $message->from('reminder@example.com', 'reeminder'); $message->to($to,'')->subject('happy birthday you!'); } ); } }
how dispatch
command
every 1 hour schedular
laravel doc shows example of scheduling console commands, not commands under app/commands
diretory
edit 1
to more specific, dispatch sendbirthdayemailcommand
schedular
correct approach? or have create console command explicitly , make call command in app/commands
p.s. 2 command references confusing. commands
in /app/console/commands
artisan console commands
call commands
in /app/commands
directory handler
classes in /app/handlers
directory
edit 2
as suggested @luceos here https://stackoverflow.com/a/31043897/1679510
i have used closure below dispatch sendbirthdayemailcommand
/app/console/kernel.php
protected function schedule(schedule $schedule) { $schedule->command('inspire') ->hourly(); $schedule->call(function() { $this->dispatch(new app\commands\sendbirthdayemailcommand()); })->hourly(); }
and avail dispatch()
method in kernel have did following
availing
command bus facade
kernel
use illuminate\foundation\bus\dispatchescommands;
using facade inside
kernel
classclass kernel extends consolekernel { use dispatchescommands; /* rest of code */ }
let's see if works!! figuring out how run cron on xampp let schedular run.
also unsure, debug log if there comes error
update:
after bit playing kernel , running php artisan
have found only console commands can registered in kernel
for example
class kernel extends consolekernel { protected $commands = [ 'app\console\commands\inspire', '\app\commands\sendbirthdayemailcommand', /* give error */ ]; }
the commands under app/commands/
directory cannot registered in kernel, accepts instance of console commands
so said, if call app/commands/mycommand
command in schedule
method $schedule->command('mycmd')
do have create explicit console command?
you edit app/console/kernel.php
class , add command under schedule()
method using:
$schedule->command('birthday-email')->everyhour();
assuming named app\commands\sendbirthdayemailcommand
birthday-email
using property $name
.
as have 2 classes think mixed things up. need move handle()
method of sendbirthdayemailcommandhandler
sendbirthdayemailcommand
or call method within command.
if want call arbitrary things can use call method:
schedule->call(function() { // task... })->hourly();
Comments
Post a Comment