Laravel5.5 综合使用
使用 Laravel5.5 开发一个自动交割的项目,把使用到的开源扩展包及特性整理起来,以供后续使用。
一、安装IDE提示工具
Laravel IDE Helper 是一个极其好用的代码提示及补全工具,可以给编写代码带来极大的便利。
1、安装
# 如果只想在开发环境安装请加上 --dev
composer require barryvdh/laravel-ide-helper
安装 doctrine/dbal 「请装上它,在为模型注释字段的时候必须用到它」
# 如果只想在开发环境安装请加上 --dev
composer require "doctrine/dbal: ~2.3"
详细安装方法,请参考这篇博文: Laravel 超好用代码提示工具 Laravel IDE Helper
三个常用命令
- php artisan ide-helper:generate - 为 Facades 生成注释
- php artisan ide-helper:models - 为数据模型生成注释
- php artisan ide-helper:meta - 生成 PhpStorm Meta file
二、Monolog日志包
日志的重要程度不言而喻, 不管是在开发过程中, 还是部署到生产环境后, 都是经常使用的.
随着 psr-3
的出现, 终于统一了 php
中日志的风格.但是, 好用的记录日志系统, 也很重要.monolog
是我遇到的最好的日志系统.而且, laravel 中也是用的 monolog
。
安装
composer require monolog/monolog
用法
Github地址:monolog/monolog
<?php
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
// create a log channel
$log = new Logger('name');
$log->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING));
// $logger->pushHandler(new StreamHandler(storage_path() . '/logs/spider.log'));
// add records to the log
$log->warning('Foo');
$log->error('Bar');
三、抓包工具
Guzzle 是一个十分强大的php的模拟HTTP client的第三方库,可以通过composer安装
Goutte 是一个用来解析HTML文档的第三方库,可以通过composer安装
安装
composer require fabpot/goutte
composer require guzzlehttp/guzzle
创建命令
php artisan make:command Spider
命令参数
// concurrency为并发数 keyWords为查询关键词
protected $signature = 'command:spider {concurrency} {keyWords*}';
实战
<?php
namespace App\Console\Commands;
use Goutte\Client as GoutteClient;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Pool;
use Illuminate\Console\Command;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
class Spider extends Command
{
private $totalPageCount;
private $counter = 1;
private $concurrency = 7; // 同时并发抓取
private $logger = null;
private $urls = [
'https://www.feixiaohao.com/currencies/bitcoin/', // BTC
'https://www.feixiaohao.com/currencies/decred/', // DCR
];
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'test:spider-request'; //concurrency为并发数 keyWords为查询关键词
/**
* The console command description.
*
* @var string
*/
protected $description = 'php spider';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// 实例化一个日志实例, 参数是 channel name
$logger = new Logger('spider');
$logger->pushHandler(new StreamHandler(storage_path() . '/logs/spider.log'));
$this->totalPageCount = count($this->urls);
$guzzleClent = new GuzzleClient();
$client = new GoutteClient();
$client->setClient($guzzleClent);
$request = function ($total) use ($client){
foreach ($this->urls as $url){
yield function () use($client, $url){
return $client->request('GET',$url);
};
}
};
// @DOC http://docs.guzzlephp.org/en/stable/quickstart.html?highlight=pool
// /Users/kaiyiwang/Code/digcoin/vendor/symfony/dom-crawler/Crawler.php
$pool = new Pool($guzzleClent,$request($this->totalPageCount), [
'concurrency' => $this->concurrency,
'fulfilled' => function ($response, $index) use ($logger){
$res = $response->html();
// print_r($res);
$logger->info($res);
$this->info("请求第 $index 个请求,连接 " . $this->urls[$index]);
$this->countedAndCheckEnded();
},
'rejected' => function ($reason, $index){
$this->error("rejected" );
$this->error("rejected reason: " . $reason );
$this->countedAndCheckEnded();
},
]);
// 开始发送请求
$promise = $pool->promise();
$promise->wait();
}
public function countedAndCheckEnded()
{
if ($this->counter < $this->totalPageCount){
$this->counter++;
return;
}
$this->info("请求结束!");
}
// 运行命令:php artisan test:spider-request
}
四、定时任务
CRON是一个守护进程,它驻留在你的linux服务器中,大部分时间都没有唤醒,但是每一分钟它都会睁开双眼,看看是否运行任何给定的任务,你使用crontab文件与该守护进程通信,在大多数常见的设置文件可以位于/etc/crontab
,crontab文件可能看起来像这样:
0 0 1 * * /home/full-backup
0 0 * * * /home/partial-backup
30 5 10 * * /home/check-subscriptions
1.添加系统定时任务
在laravel中添加定时任务很简单,首先在系统crontab 添加一个artisan的定时任务,每分钟执行一次。
> crontab -e
// /home/vagrant/Code/digcoin/ laravel项目在服务器的地址
* * * * * php /home/vagrant/Code/digcoin/artisan schedule:run >> /dev/null 2>&1
2.项目中添加定时命令
在 App\Console\Kernel
类的 schedule
方法中定义预定的命令:
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
// php artisan test:spider-request, 每十分钟调用一次
$schedule->command('test:spider-request')
->everyFifteenMinutes()->withoutOverlapping();
}
添加好了之后,我们可以直接使用这个命令测试定时任务是否可以执行:
> php /home/vagrant/Code/digcoin/artisan test:spider-request
OK,只需要简单的两步便可实现laravel的定时任务添加。
更多关于Laravel的任务调度,请看考该文:Laravel 的任务调度(计划任务)功能 Task Scheduling
原文地址:https://segmentfault.com/a/1190000015968429
Laravel5.5 综合使用的更多相关文章
- 【技术博客】 关于laravel5.1中文件上传测试的若干尝试
关于laravel5.1中文件上传测试的若干尝试 作者:ZGJ 版本:v1.0 PM注:本人这两天也正在尝试解决这一问题,如有进展将及时更新这一博客 在我们的软工第二阶段中,我开始着手进行后端控制器的 ...
- AEAI DP V3.6.0 升级说明,开源综合应用开发平台
AEAI DP综合应用开发平台是一款扩展开发工具,专门用于开发MIS类的Java Web应用,本次发版的AEAI DP_v3.6.0版本为AEAI DP _v3.5.0版本的升级版本,该产品现已开源并 ...
- H5+JS+CSS3 综合应用
慕课网新教程H5+JS+CSS3 实现的七夕言情主题效果已经出炉了 从设计到实现到录制与编写用了快1个月的时间,说真的这个案例是慕课定制的,我之前也没有系统的做过这样的一个效果,在实现的时候自己也重新 ...
- [教程] [授权翻译] 使用补丁修改DSDT/SSDT [DSDT/SSDT综合教程]
[教程] [授权翻译] 使用补丁修改DSDT/SSDT [DSDT/SSDT综合教程] http://bbs.pcbeta.com/viewthread-1571455-1-1.html [教程] [ ...
- laravel5 安装笔记
1.环境更新 apt-get update apt-get install php5-cli apt-get install curl 2. Composer安装 curl -sS https://g ...
- Laravel5路由/home页面无法访问
报错信息: Not Found The requested URL /laravel5/public/home was not found on this server. 解决方法: 1.编辑apac ...
- Laravel5.0学习--03 Artisan命令
本文以laravel5.0.22为例. 简介 Artisan 是 Laravel 内置的命令行接口.它提供了一些有用的命令协助您开发,它是由强大的 Symfony Console 组件所驱动.利用它, ...
- iOS--知识综合应用成就时髦小功能点
iOS--知识综合应用成就时髦小功能点
- Oracle 数据库基础学习 (七) SQL语句综合练习
一.多表查询综合练习 1. 列出高于在30部门工作的所有人员的薪金的员工的姓名.部门名称.部门编号.部门人数 分析: 需要的员工信息: |-emp表:姓名.部门编号 |-dept表:部门名称.部门编 ...
随机推荐
- 【bug】【userAgent】极速模式与非极速模式存在差异
UC浏览器 Android 极速模式 UC浏览器 Android 非极速模式
- 杂项-Java:Ehcache
ylbtech-杂项-Java:Ehcache EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. 1.返回顶部 1. 基 ...
- JS如何遍历一个文件夹下的所有文件与目录(转)
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- /bin/bash: jar: command not found(转载)
转自:http://blog.csdn.net/zhangdaiscott/article/details/23138023 /bin/bash: jar: command not found 解决办 ...
- bzoj 1783: [Usaco2010 Jan]Taking Turns【贪心+dp】
不知道该叫贪心还是dp 倒着来,记f[0][i],f[1][i]分别为先手和后手从n走到i的最大值.先手显然是取最大的,当后手取到比先手大的时候就交换 #include<iostream> ...
- SpringBoot整合SpringSecurity简单案例
在我们开发项目的过程中经常会用到一些权限管理框架,Java领域里边经常用的可能就是shiro了,与之对应的还有SpringSecurity,SpringSecurity可以说是非常强大,与Spring ...
- python 中 str与bytes的转换
# bytes转字符串方式一 b=b'\xe9\x80\x86\xe7\x81\xab' string=str(b,'utf-8') print(string) # bytes转字符串方式二 b=b' ...
- daily_journal_3 the game of thrones
昨晚追完了最爱的美剧(the game of thrones),哇,看到结局有点崩溃.果然还是美帝淫民开放,各种乱伦,在七夕收到的万点暴击就祝天下有情人就像剧中一样终是血亲. 昨天算是完成了git的复 ...
- 289 Game of Life 生命的游戏
假设有一个大小为m*n的板子,有m行,n列个细胞.每个细胞有一个初始的状态,死亡或者存活.每个细胞和它的邻居(垂直,水平以及对角线).互动规则如下:1.当前细胞存活时,周围低于2个存活细胞时,该细胞死 ...
- working hard to be a professional coder
1:read 2 : code 3 : 勤奋 4:技术栈 就前端主流技术框架的发展而言,过去的几年里发展极快,在填补原有技术框架空白和不足的同时也渐渐趋于成熟.未来前端在已经趋向成熟的技术方向上面将会 ...