A Cron Job in Laravel 4
Solution 1
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
Solution 2
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 ;)
Bryan Villafañe
Updated on June 10, 2022Comments
-
Bryan Villafañe almost 2 years
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
-
Arda over 10 yearsAlternatively, 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.
-
itachi over 10 years@Arda can you explain that approach a little? any info on that? i never understood the purpose of iron.
-
Arda over 10 years@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.
-
Snapey about 10 yearsI 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.
-
Sameer Shaikh over 9 yearsi am doing this in my terminal 'php artisan cronjob:execute' but it is giving me the error runtimeException.. not enough arguments..
-
ajtrichards over 9 yearsTo run the command in your terminal you'd need to run
php artisan command:my_foo_command
. Withmy_foo_command
being the name of the command you've setup in your command file.