Reputation: 678
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
Upvotes: 5
Views: 11723
Reputation: 30565
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
Upvotes: 0
Reputation: 22862
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 ;)
Upvotes: 3
Reputation: 1911
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
Upvotes: 10