简体   繁体   English

Laravel 4中的Cron工作

[英]A Cron Job in Laravel 4

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 问题是,当我设置到该控制器的路由时,它可以工作

Controllers don't run by themselves, they work as a component of Laravel. 控制器不是自己运行的,它们是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 Controller class, does not exist. 如果你直接加载你的控制器,那么Laravel没有加载,就PHP而言, BaseController以及Laravel的Controller类不存在。 Normally your web server loads public/index.php which loads Laravel and so on. 通常,您的Web服务器会加载public/index.php ,它会加载Laravel等等。 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/ 如果这令人困惑,您可能想了解如何使用Composer进行自动加载: http//net.tutsplus.com/tutorials/php/easy-package-management-with-composer/

What you should do is write an Artisan command that does what you need and call that command using cron. 你应该做的是编写一个Artisan命令来完成你需要的命令并使用cron调用该命令。 This question gives details on how to accomplish this: Cron Job in Laravel 这个问题提供了如何实现这一目标的详细信息: Laravel中的Cron Job

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 ;) 有机会,您可以将整个控制器方法放入此命令而无需触摸代码;)

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. 首先,使用Artisan创建一个新命令。 From the command line type: 从命令行类型:

php artisan command:make FooCommand

In your app/commands folder 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() . 打开该文件并在函数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. 还有一些其他函数允许您从命令行捕获参数和选项。

In your command file there are also $name and $description variables 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.php and adding: 完成后,您需要通过打开app/start/artisan.php并添加以下内容将其注册到Artisan:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM