How to make function run in background in laravel(laravel如何让函数在后台运行)
问题描述
我正在使用 Laravel 5.0 开发一个网站并托管在 Windows Server2012 中.
I am developing a website in Laravel 5.0 and hosted in Windows Server2012.
我遇到了一个问题,即我正在从另一个函数 A 调用控制器中的函数 B 并且我希望调用另一个函数 B 的函数 A 不等待函数 B 的完成.并且函数 B 在后台完成并独立形成页面的用户终止和函数 A 返回.
I am stuck at a problem which is I am calling a function B in controller from another function A and I want that the function A which calls the another function B does not wait for the completion of function B . And Function B gets completes in the background and independent form user termination of page and function A return .
我对此进行了搜索,发现这可以通过 cron 来实现,例如 windows 中的作业、pcntl_fork() 和 laravel 中的队列功能.我是这一切的初学者.
I have searched this and found that this can be implemented through cron like jobs in windows, pcntl_fork() and Queue functionality in laravel. I am beginner in all this.
请帮忙!提前致谢.
推荐答案
如文档所述 http://laravel.com/docs/5.1/queues,首先你需要设置驱动程序 - 我会在开始时使用数据库:
as the documentation states http://laravel.com/docs/5.1/queues, first you need to setup the driver - i would go for database in the beginning :
php artisan queue:table
php artisan migrate
然后创建要添加到队列中的作业
then create the Job that you want to add to the queue
<?php
namespace AppJobs;
use AppUser;
use AppJobsJob;
use IlluminateContractsMailMailer;
use IlluminateQueueSerializesModels;
use IlluminateQueueInteractsWithQueue;
use IlluminateContractsBusSelfHandling;
use IlluminateContractsQueueShouldQueue;
class SendEmail extends Job implements SelfHandling, ShouldQueue
{
use InteractsWithQueue, SerializesModels;
protected $user;
public function __construct(User $user)
{
$this->user = $user;
}
public function handle(Mailer $mailer)
{
$mailer->send('emails.hello', ['user' => $this->user], function ($m) {
//
});
}
}
然后在 Controller 中调度作业
then in the Controller dispatch the job
<?php
namespace AppHttpControllers;
use AppUser;
use IlluminateHttpRequest;
use AppJobsSendReminderEmail;
use AppHttpControllersController;
class UserController extends Controller
{
/**
* Send a reminder e-mail to a given user.
*
* @param Request $request
* @param int $id
* @return Response
*/
public function sendReminderEmail(Request $request, $id)
{
$user = User::findOrFail($id);
$sendEmailJob = new SendEmail($user);
// or if you want a specific queue
$sendEmailJob = (new SendEmail($user))->onQueue('emails');
// or if you want to delay it
$sendEmailJob = (new SendEmail($user))->delay(30); // seconds
$this->dispatch($sendEmailJob);
}
}
为此,您需要运行队列侦听器:
For that to work, you need to be running the Queue Listener:
php artisan queue:listen
这有答案吗?
这篇关于laravel如何让函数在后台运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:laravel如何让函数在后台运行
基础教程推荐
- 在PHP中根据W3C规范Unicode 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何替换eregi() 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
