tom*_*rod 32
下面我详细使用教程commands中Laravel 4:用cron.我已经分为四个步骤,以便更容易理解.
php artisan command:make RefreshStats
Run Code Online (Sandbox Code Playgroud)
使用上面的命令,Laravel将创建一个RefreshStats.php在目录中命名的文件 app/commands/
RefreshStats.php它是这样的文件:
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),
);
}
}
Run Code Online (Sandbox Code Playgroud)
你应该改变这一行:
protected $name = 'command:name';
Run Code Online (Sandbox Code Playgroud)
这样的事情:
protected $name = 'refresh:stats';
Run Code Online (Sandbox Code Playgroud)
如果您不需要参数(选项相同),请更改以下行:
protected function getArguments() {
return array(
array('example', InputArgument::REQUIRED, 'An example argument.'),
);
}
Run Code Online (Sandbox Code Playgroud)
至:
protected function getArguments() {
return array();
}
Run Code Online (Sandbox Code Playgroud)
而现在注重的fire功能.该命令将执行在该函数中编写的源代码.例:
public function fire(){
echo "Hello world";
}
Run Code Online (Sandbox Code Playgroud)
您需要注册该命令.所以打开app/start/artisan.php文件,并添加一行如下:
Artisan::add(new RefreshStats);
Run Code Online (Sandbox Code Playgroud)
最后,您可以添加计划任务,如下所示:
crontab -e
Run Code Online (Sandbox Code Playgroud)
并添加一行(每30分钟运行一次命令),如下所示:
*/30 * * * * php path_laravel_project/artisan refresh:stats
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23460 次 |
| 最近记录: |