Laravel 采坑

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,

时间: 2024-08-07 02:52:56

Laravel 采坑的相关文章

Laravel Homestead 安装与采坑

1.安装链接 教程https://laravel-china.org/docs/laravel-development-environment/5.5/development-environment-windows/938 2.采坑https://laravel-china.org/articles/4082/a-wonderful-problem-encountered-in-the-use-of-vagrant 解决方法:这个解决方法可以,我改成了 GBK,在 Windows 10 下. 

phoenegap3.5 采坑

上周5晚上在家看Node.js视频的时候,老大来一条短信让研究下 phoengap打包一个web网站. 遂 陷入了phonegap的深渊中. phoengap很早开始使用 cli模式安装开发环境 ,借助node npm来管理 环境要求: JAVA Android SDK Ant Node.js 以上几个的下载地址就不提供. 主要是这中间一些坑,装好上面的环境 需要自己手动配置环境变量, 痛苦由此开始啊!!!! 多次操作修改环境变量 都没能成功.多次卸载,遇到各种BUG. 在修改环境变量的时候 往

oracle rac 扩展磁盘采坑记录

oracle rac 磁盘扩展采坑记录: 系统环境:VMware esxi oracle rac 11.2.0.4 昨天扩展了asm的磁盘组,FRA磁盘组和DATA磁盘组,我添加了一块硬盘320g,200g扩展到了DATA组,120g扩展到了FRA组.因为oracle使用的是raw裸设备,扩展第一个磁盘的时候,正常进行,不用关机. 坑1:但是扩展FRA组的时候,需要关机才能找得到第二个设备.重启之后扩展正常,建议扩展磁盘一次扩展到一个组. 坑二:今天我看见系统盘空间不够了,就扩展了一下系统盘空间

采坑:python base64

需求:  读取文本内容,对字符串进行base64加密 >>> str = 'aaaaaaaaaaaaaaaaaaa\nbbbbbbbbbbbbbbbbbbbbbbbbbbb\ncccccccccccccccccccccccccc' >>> encodeStr = base64.encodestring(str) >>> encodeStr 'YWFhYWFhYWFhYWFhYWFhYWFhYQpiYmJiYmJiYmJiYmJiYmJiYmJiYmJi

websocket采坑记

项目中想用做个实时统计,像是110警情大屏那种,所以用到了websocket,结果踩了不少坑,再次记录下. 环境:spring,springMVC(4.2.4.RELEASE),tomcat7 问题1:session对象是不一样的 http的时候,是javax.servlet.http.HttpSession 而websocket的时候javax.websocket.Session http的session一般用于保存用户信息,根据用户,http请求的时候携带Cookie:JSESSIONID,

VS2015+MySql+EF6采坑经验总结

背景:VS2015+MySql+EF6(DB First) 采坑顺序:按照以前的记忆,操作依次如下: 1,安装 MySQL Connector/NET(不用想,装最新的,8.0.12) 2.安装 MySQL for Visual Studio (最新1.2.8) 3.在VS2015创建WEB项目 4.nuget 安装MySql.Data.Entity (最新6.10.8,会自动安装依赖包MySql.Data 6.10.8 和 EF 6.2.0) 雷区:下来,当然的要创建[ADO.NET实体数据模

github 采坑记 —— 项目提交到github后部分文件缺失

在使用git push到GitHub上后,发现部分文件缺失,如下图所示: 可以看到dist文件夹为 运行 npm run build 之后打包生成的文件,node_modules 文件也是缺失的 导致文件没有提交的原因是在项目根目录下有个文件: 打开文件: 可以看再提交时有些文件被忽略了,可以将相应代码删除,然后重新push到GitHub上 想要的文件就提交上去了! 多多采坑,多多总结,多多练习~~~ 原文地址:https://www.cnblogs.com/amy2017/p/10087455

Axios采坑之路

Axios采坑之路 POST请求设置Content-Type 由于后端采用的是form表单形式上送参数,需要设置Content-Type axios设置如下 const _axios = axios.create(config); _axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;'; api调用 import { request } from '@plugin/axios' exp

一起采坑redis(2)--redis 配置

单机配置(slave的配置就多红色部分,slaveof,masterauth) bind 192.168.50.92 protected-mode yes port 6399 tcp-backlog 511 timeout 300 tcp-keepalive 300 daemonize yes supervised no pidfile /data/redis_data/run/redis_6399.pid loglevel notice logfile "/data/redis_data/re