Laravel 4 中的 Cron 作业

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19150107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:30:32  来源:igfitidea点击:

A Cron Job in Laravel 4

phpcronlaravel

提问by Bryan Villafa?e

I need a Cron job for execute a Scraper to a Website and send emails with the information, I made a Controller to do that, but when I set up the command to run that file

我需要一个 Cron 作业来执行一个 Scraper 到一个网站并发送带有信息的电子邮件,我做了一个控制器来做到这一点,但是当我设置命令来运行该文件时

php app/controllers/ScraperController.php 

I get this error

我收到这个错误

PHP Fatal error: Class 'BaseController' not found in /var/www/U-Scraper/app/controllers/ScraperController.php on line 2

PHP 致命错误:在第 2 行的 /var/www/U-Scraper/app/controllers/ScraperController.php 中找不到“BaseController”类

The thing is, it works when I set up with a route to that controller

问题是,当我设置到该控制器的路由时它可以工作

回答by Brian Ortiz

Controllers don't run by themselves, they work as a component of Laravel. If you're loading your controller directly then Laravel is not being loaded and as far as PHP is concerned BaseController, as well as Laravel's Controllerclass, does not exist. Normally your web server loads public/index.phpwhich loads Laravel and so on. If that's confusing you may want to learn about how autoloading with Composer works: http://net.tutsplus.com/tutorials/php/easy-package-management-with-composer/

控制器不自己运行,它们作为 Laravel 的一个组件工作。如果您直接加载控制器,则不会加载 Laravel,并且就 PHP 而言BaseController,以及 Laravel 的Controller类不存在。通常你的 Web 服务器会public/index.php加载 Laravel 等等。如果这令人困惑,您可能想了解 Composer 自动加载的工作原理:http: //net.tutsplus.com/tutorials/php/easy-package-management-with-composer/

What you should do is write an Artisan commandthat does what you need and call that command using cron. This question gives details on how to accomplish this: Cron Job in Laravel

您应该做的是编写一个 Artisan 命令来执行您需要的操作并使用 cron 调用该命令。这个问题详细说明了如何做到这一点:Laravel 中的 Cron Job

回答by Andreyco

I suggest you to create new Artisan command instead of controller.

我建议您创建新的 Artisan 命令而不是控制器。

Then set CRON task to run your command, for example:

然后设置 CRON 任务来运行你的命令,例如:

1 * * * * /usr/bin/php /path/to/the/artisan nameofthecustomcommand

If you cannot run/set task this way, but you can set the URL to execute

如果你不能以这种方式运行/设置任务,但你可以设置 URL 来执行

http://mydomain.com/cron.php

// cron.php
// I am aware I do use exec()
exec('php artisan nameofthecustomcommand');

More about Artisan commands here

更多关于 Artisan 命令的信息在这里

There is a chance, you can put whole controller method into this command without having to touch the code ;)

有机会,您可以将整个控制器方法放入此命令中,而无需触摸代码;)

回答by ajtrichards

This is the way i've setup CRON jobs using Laravel 4 and the Artisan Command function.

这是我使用 Laravel 4 和 Artisan Command 功能设置 CRON 作业的方式。

Firstly, create a new command using Artisan. From the command line type:

首先,使用 Artisan 创建一个新命令。从命令行输入:

php artisan command:make FooCommand

php artisan command:make FooCommand

In your app/commandsfolder you will now have a new file called FooCommand.php.

在您的app/commands文件夹中,您现在将拥有一个名为FooCommand.php.

Open that file up and write your code in the function fire(). This will run every time your command runs. There are some other functions that allow you to capture arguments and options from the command line.

打开该文件并在函数中编写您的代码fire()。每次您的命令运行时,这都会运行。还有一些其他函数允许您从命令行捕获参数和选项。

In your command file there are also $nameand $descriptionvariables that need to be filled in. Give your task a nice name and description like:

在命令文件中也有$name$description需要填写给你的任务,一个漂亮的名称和说明变量一样:

/**
 * The console command name.
 *
 * @var string
 */
protected $name = 'command:my_foo_command';

/**
 * The console command description.
 *
 * @var string
 */
protected $description = 'A description of what the command does';

Once you've finished you need to register it to Artisan by opening up app/start/artisan.phpand adding:

完成后,您需要通过打开app/start/artisan.php并添加以下内容将其注册到 Artisan :

Artisan::add(new FooCommand);

Artisan::add(new FooCommand);

Then using Artisan in the command line you can run your task using:

然后在命令行中使用 Artisan,您可以使用以下命令运行您的任务:

php artisan command:my_foo_command

This will only invoke the command once - to get it running on a regular basis add the following to your CRONTAB:

这只会调用该命令一次 - 要使其定期运行,请将以下内容添加到您的 CRONTAB 中:

1 * * * * /usr/bin/php /path/to/the/artisan command:my_foo_command