Laravel¿ª·¢£ºÔõÑùʹÓÃLaravel Task SchedulingÍýÏëʹÃü£¿
laravelÊÇÒ»¸öÊ¢ÐеÄphp¿ò¼Ü£¬ËüΪwebÓ¦ÓóÌÐòµÄ¿ª·¢ÌṩÁ˺ܺõÄÖ§³Ö¡£ÆäÖÐÒ»¸ö¹¦Ð§Ç¿Ê¢ÇÒÊÊÓõÄÌØÕ÷ÊÇlaravel task schedulingÍýÏëʹÃü¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃlaravel task schedulingÀ´Ö´ÐÐ׼ʱʹÃü¡£
Ò»¡¢Ê²Ã´ÊÇLaravel Task SchedulingÍýÏëʹÃü£¿
Laravel Task SchedulingÊÇÒ»ÖÖ¿ò¼ÜÌṩµÄÖ´ÐÐ׼ʱʹÃüµÄ·½·¨¡£Í¨¹ýËü¿ÉÒÔÉèÖÃÖÖÖÖ׼ʱʹÃü£¬ÀýÈçÿСʱ¡¢ÖðÈÕ¡¢Ã¿ÖÜ»òÿÔÂÖ´ÐÐÒ»´Îij¸öº¯Êý»ò¾ç±¾£¬¶øÎÞÐèÊÖ¶¯Ö´ÐС£ÕâÔÚÖÖÖÖwebÓ¦ÓóÌÐòÖж¼ÊǺÜÊÇÓÐÓõģ¬ÀýÈ磺ÐèÒªÌìÌ챸·ÝÊý¾Ý¡¢Ã¿ÖÜ·¢ËÍÒ»´ÎÐÂÎÅÐŵȡ£
ʹÓÃLaravel Task SchedulingʹµÃ±àдºÍµ÷ÀíÕâЩ׼ʱʹÃü±äµÃÏà¶Ô¼òÆÓ£¬²¢ÇÒʹµÃ´úÂëÕûÆë¸ßЧ¡£
¶þ¡¢ÔõÑùÔÚLaravelÖÐʹÓÃTask Scheduling£¿
1.×¢²áÍýÏëʹÃü
ҪʹÓÃLaravel Task Scheduling£¬Ê×ÏÈÐèÒª¾ÙÐÐһЩÉèÖᣠ·¿ªapp/Console/Kernel.phpÎļþ²¢ÏòscheduleÒªÁìÖÐÌí¼ÓÍýÏëʹÃü¡£¿ÉÒÔÔÚÕâ¸öÒªÁìÖÐ×¢²áscheduleº¯ÊýµÄËùÓÐÍýÏëʹÃü¡£
/**
Define the application’s command schedule.
*
@return void
*/
protected function schedule(Schedule $schedule)
{
//ÌìÌìÆÆÏþÖ´ÐÐʹÃü $schedule->call(function () { DB::table('recent_users')->delete(); })->daily();
µÇ¼ºó¸´ÖÆ
}
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃǽ¨ÉèÁËÒ»¸öÄäÃûº¯Êý£¬ÆäÖÐŲÓÃÁËһЩÆäËûÒªÁìÀ´É¾³ý¡°recent_users¡±Êý¾Ý¿â±íÖеÄÊý¾Ý¡£Õâ¸öʹÃü±»Ö¸¶¨ÔÚÌìÌìÆÆÏþÖ´ÐÐÒ»´Î¡£
2.Æô¶¯µ÷Àí
ÒªÆô¶¯ÍýÏëʹÃü£¬ÔÚЧÀÍÆ÷ÖпÉÒÔʹÓÃcrontab¡£¿ÉÊÇ£¬LaravelTask SchedulingÌṩÁËÀû±ãµÄÏÂÁîÐз½·¨ÔÚºǫ́Æô¶¯ÕâЩʹÃü¡£
ÔÚÖն˴°¿ÚÖнøÈëLaravelÓ¦ÓóÌÐòµÄ¸ùĿ¼£¬²¢ÊäÈëÒÔÏÂÏÂÁ
php artisan schedule:run
Õâ¸öÏÂÁî»áÆô¶¯Ò»¸öºǫ́Àú³Ì£¬±àÅÅÏÈÇ°ÉèÖõÄ׼ʱʹÃü¡£ ÈôÊDZ£´æÈκÎÍýÏëʹÃü£¬ËüÃǻᱻ׼ʱÔËÐС£
3.ÔöÌíÍýÏëʹÃü
ÔÚLaravelÖÐÔöÌíÍýÏëʹÃüÏà¶ÔÈÝÒ×£¬ÔÚscheduleÒªÁìÖмÓÈëһЩÐÂʹÃü¼´¿É¡£ÀýÈ磬¿ÉÒÔ½«ÒÔϺ¯ÊýÌí¼Óµ½scheduleÖУº
/**
Register the commands for the application.
*
@return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands'); require base_path('routes/console.php');
µÇ¼ºó¸´ÖÆ
}
$schedule->command(’emails:send’, [‘–force’])->daily();
¸ÃÏÂÁîʹÓà –force ²ÎÊýÀ´Ç¿ÖÆÖ´ÐÐת´ï¸øcommandµ÷ÀíÆ÷µÄÏÂÁî¡£ÔÚÕâ¸öÀý×ÓÖУ¬Ö»ÓÐÔÚÌìÌìÒ¹¼äʱ²Å»áÖ´ÐиÃÏÂÁî¡£
Èý¡¢½áÂÛ
Laravel Task SchedulingÊÇLaravel¿ò¼ÜÖеÄÒ»¸öÇ¿Ê¢µÄ¹¦Ð§£¬¿ÉÒÔ×ÊÖúÄã¼ò»¯×¼Ê±Ê¹ÃüµÄ±àдºÍµ÷Àí¡£Í¨¹ý¸Ã¹¦Ð§£¬¿ÉÒÔÇáËɵؽ«Ò»Ð©Òªº¦Ê¹Ãü×Ô¶¯»¯£¬²¢È·±£ËüÃÇÔÚÔ¤¶¨µÄʱ¼äÄÚÖ´ÐС£ÈôÊÇÄãÕýÔÚÑ°ÕÒÒ»ÖÖ¿É¿¿µÄʹÃüµ÷Àí·½·¨£¬Laravel Task SchedulingÊÇÖµµÃʵÑéµÄÒ»¸öÑ¡Ôñ¡£
ÒÔÉϾÍÊÇLaravel¿ª·¢£ºÔõÑùʹÓÃLaravel Task SchedulingÍýÏëʹÃü£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡