5

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

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

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

peterm
  • 91,357
  • 15
  • 148
  • 157
Bryan Villafañe
  • 678
  • 3
  • 8
  • 18

3 Answers3

10

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 Controller class, does not exist. Normally your web server loads public/index.php which 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/

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

Community
  • 1
  • 1
Brian Ortiz
  • 1,821
  • 1
  • 20
  • 47
  • Alternatively, you can also use a service like setcronjob + iron.io push queue. I use it in the production servers where I don't have access to crontab. – Arda Oct 03 '13 at 09:31
  • @Arda can you explain that approach a little? any info on that? i never understood the purpose of iron. – itachi Oct 03 '13 at 13:36
  • 1
    @itachi Think like this: in my example, what iron.io does is goes to a request and waits for you to complete. You make a conjob request to setcronjob, on the time, the cronjob service triggers the queue engine, queue engine makes a push request to your action and waits until it's completed. Cronjob service can also wait (setcronjob can only wait like 15 seconds if I recall correctly), but your request may take longer time (imagine something like mass emailing with a remote SMTP service etc. and sending emails to 1000 individuals), so queueing brings benefit here. – Arda Oct 03 '13 at 13:41
3

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

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

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

http://mydomain.com/cron.php

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

More about Artisan commands here

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

Andreyco
  • 22,476
  • 5
  • 61
  • 65
  • I agree, creating artisan command is probably the best, but in some circumstances CRON of a LYNX command can be used - especially if you want the option to also run the same task from a browser. – Snapey Mar 07 '14 at 18:55
0

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

Firstly, create a new command using 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.

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.

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:

/**
 * 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:

Artisan::add(new FooCommand);

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

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:

1 * * * * /usr/bin/php /path/to/the/artisan command:my_foo_command
ajtrichards
  • 29,723
  • 13
  • 94
  • 101
  • i am doing this in my terminal 'php artisan cronjob:execute' but it is giving me the error runtimeException.. not enough arguments.. – Sameer Shaikh Jan 08 '15 at 11:59
  • To run the command in your terminal you'd need to run `php artisan command:my_foo_command`. With `my_foo_command` being the name of the command you've setup in your command file. – ajtrichards Jan 08 '15 at 14:34