Laravel 采坑
阿新 • • 發佈:2017-12-22
signature oid ont min var data requests acad artisan
2017年12月22日17:40:03
版本5.4.X
一下是可能會遇到的坑
1,必須的寫路由轉發才能訪問控制器,當然你可以自動路由訪問,但是需要些匹配規則,其實還是轉發了
2,Laravel 做計劃任務的時候坑真的好多,比如不能直接跨控制器訪問,web的是web的路由,console是它自己的,所以你的功能和邏輯代碼必須在Repository或者service裏面,不然你懂的,做好邏輯代碼分離
官方文檔只有用過的才能看得懂,我很無奈
完整流程
app\Console\Commands下建立你的任務文件
SpiderTask.php
<?php namespace App\Console\Commands;use Illuminate\Console\Command; //use Illuminate\Support\Facades\Redis; use App\Repositories\SpiderRepository;//具體邏輯代碼 class SpiderTask extends Command { protected $taskserver; /** * The name and signature of the console command. * * @var string */ protected $signature = ‘SpiderTask‘;/** * The console command description. * * @var string */ protected $description = ‘spider every one hour crawl data‘; protected $spider; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct();$this->spider = new SpiderRepository(); } /** * Execute the console command. * * @return mixed */ public function handle() { $this->spider->do_all();//具體執行地方 } }
然後註冊到Kernel.php
<?php namespace App\Console; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; use App\Repositories\SpiderRepository;//我的具體邏輯代碼地方 class Kernel extends ConsoleKernel { /** * The Artisan commands provided by your application. * * @var array */ protected $commands = [ // ‘App\Console\Commands\SpiderTask‘, //必須 ]; /** * Define the application‘s command schedule. * * @param \Illuminate\Console\Scheduling\Schedule $schedule * @return void */ protected function schedule(Schedule $schedule) { // $schedule->call(function () { // $Spider = new SpiderRepository(); // $Spider->do_all(); // })->daily(); $schedule->command(‘SpiderTask‘)->daily();
//兩種方法都可以,建議第二種,邏輯更清晰 } /** * Register the Closure based commands for the application. * * @return void */ protected function commands() { require base_path(‘routes/console.php‘); } }
註意:
你要測試你的代碼邏輯有沒有錯誤
最好在Linux下測試,因為windows好多問題
在代碼根目錄,如果命令沒有到全局,使用完整路徑
php artisan schedule:run
everyMinute才會實時運行,可以看到報錯
http://laravelacademy.org/post/6931.html 官方文檔
<?php namespace App\Repositories; use App\Models\Spider; //use phpspider\core\phpspider; use phpspider\core\requests; use phpspider\core\selector; class SpiderRepository { use BaseRepository; protected $model; /** * ActivityRepository constructor. * @param Activity $activity */ public function __construct() { $this->model = new Spider(); } public function do_all() { $this->cjysjs(); $this->shysjs(); $this->nchn(); $this->ltbj(); } //長江有色金屬 public function cjysjs() { $html = requests::get(‘http://www.ccmn.cn/‘); $data = selector::select($html, "#40288092327140f601327141c0560001", "css"); $data1 = selector::select($data, "tr", "css"); array_shift($data1); $array = array(); if (!empty($data1) && is_array($data1)) { foreach ($data1 as $k => &$v) { $data2 = selector::select($v, "td", "css"); foreach ($data2 as $kk => &$vv) { $vv = str_replace(‘ ‘, ‘‘, $vv); $vv = str_replace(array("\r\n", "\r", "\n"), "", $vv); $vv = trim($vv); } $data2[‘3‘] = selector::select($data2[‘3‘], "font", "css"); unset($data2[‘6‘]); $array[] = $data2; } if (empty($array)) { $info = date("Y-m-d H:i:s", time()) . ‘:長江有色金屬抓取失敗!‘; Log::info($info); } $name = ‘cjysjs‘; $_data = []; if (!empty($array) && is_array($array)) { $_data[‘value‘] = json_encode($array); $_data[‘crawl_time‘] = time(); $count = $this->getData($name); if (empty($count)) { //增加 $_data[‘name‘] = $name; $result = $this->saveData(null, $_data); } else { //更新 $_data[‘name‘] = $name; $result = $this->saveData($name, $_data); } } } } public function saveData($name = null, $data = null) { return $this->model->updateOrCreate([‘name‘ => $name], $data); } public function getData($name) { return $this->model->where(‘name‘, $name)->count(); } }
3,
Laravel 采坑