Cron Job with Laravel 4

后端 未结 4 1964
醉酒成梦
醉酒成梦 2020-12-05 10:56

I\'m trying to find out how to set up a cron job in Laravel 4, and the command I would need to run in artisan for it.

In Laravel 3, there were

相关标签:
4条回答
  • 2020-12-05 11:39

    OK so I found this useful for setting up crons in laravel 4.2.

    0 讨论(0)
  • 2020-12-05 11:40

    Alternative, if you don't like commands there is an unofficial Laravel 4 cron package: https://github.com/liebig/cron

    0 讨论(0)
  • 2020-12-05 11:46

    Tasks have been replaced with commands, which are the same thing in Laravel 4, but integrated with Symfony's console component, and even more powerful than before.

    0 讨论(0)
  • 2020-12-05 11:50

    Below I detail a tutorial for using commands in Laravel 4 with cron. I have divided into four steps to make it easier to follow.


    STEP #1: Create a command in Laravel 4:

    php artisan command:make RefreshStats
    

    With command above, Laravel will create a file named RefreshStats.php in directory app/commands/


    RefreshStats.php it's a file like this:

    use Illuminate\Console\Command;
    use Symfony\Component\Console\Input\InputOption;
    use Symfony\Component\Console\Input\InputArgument;
    
    class RefreshStats extends Command {
    
            protected $name = 'command:name';
            protected $description = 'Command description.';
    
            public function __construct() {
                    parent::__construct();
            }
    
            public function fire(){
    
            }
    
            protected function getArguments() {
                return array(
                    array('example', InputArgument::REQUIRED, 'An example argument.'),
                );
            }
    
            protected function getOptions() {
                return array(
                    array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
                );
            }
    
    }
    



    STEP #2: Simple "configuration" of RefreshStats file:

    You should change this line:

    protected $name = 'command:name';
    

    to something like this:

    protected $name = 'refresh:stats';
    

    If you don't need arguments (same for options), change this line:

    protected function getArguments() {
          return array(
              array('example', InputArgument::REQUIRED, 'An example argument.'),
          );
    }
    

    to:

    protected function getArguments() {
          return array();
    }
    

    And now pay attention to the fire function. The command will execute source code that is wrote in that function. Example:

    public function fire(){
        echo "Hello world";    
    }
    



    STEP #3: Register the command:

    You need to register the command. So open app/start/artisan.php file, and add one line as below:

    Artisan::add(new RefreshStats);
    



    STEP #4: Create CRON scheduled task:

    Finally, you can add a scheduled task as follow:

    crontab -e
    

    And add a line (run command every 30 minutes) like below:

    */30 * * * * php path_laravel_project/artisan refresh:stats
    



    And all will work automagically!

    0 讨论(0)
提交回复
热议问题