php laravel类库相关
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php laravel类库相关相关的知识,希望对你有一定的参考价值。
# laravel类库相关
- SingleApiControllerTrait.php是给控制提供增删改查功能
## HttpCacheMiddleware
http的cache,用于http的缓存
例子:
在kernel中添加:
```php
protected $routeMiddleware = [
'http.cache' => Middleware\HttpCacheMiddleware::class,
];
```
在路由中添加:
```php
Route::group(['prefix' => 'soft'], function ($router){
/* @var \Illuminate\Routing\Router $router */
$router->get('stock/capture/img/{code}', 'HqController@stockCaptureImg')
->middleware(['http.cache:900,true']);
});
```
<?php
/**
* Created by PhpStorm.
* User: code
* Date: 2018/3/19
* Time: 下午4:55
*/
namespace App\Models;
use App\Repositories\Common\Traits\Doc\DocModelTrait;
use App\Repositories\Common\Traits\ModelScopeWhereTrait;
use App\Repositories\Common\Traits\Doc\DocModelRelationTrait;
use Illuminate\Database\Eloquent\Builder;
/**
* Class Model
*
* // $timestamps为取消时间自动更新
*
* @package App\Models
* @since \Illuminate\Database\Eloquent\Builder
*/
class Model extends \Illuminate\Database\Eloquent\Model
{
use ModelScopeWhereTrait,
DocModelRelationTrait,
DocModelTrait;
/**
* 取消更新created_at和updated_at
*
* @var bool
*/
public $timestamps = true;
/**
* 通过app获取单例方法
*
* @return $this|Builder|Builder
*/
public static function instance()
{
if (defined('SWOOLE_APP_BOOL')){ // swoole避免内存泄漏
return new static();
}
return app(static::class);
}
protected function castAttribute($key, $value)
{
if (is_null($value)) {
return $value;
}
switch ($this->getCastType($key)) {
case 'pic': // 图片,系统默认七牛
return getImgUrlPath($value);
}
return parent::castAttribute($key, $value);
}
}
<?php
/**
* Created by PhpStorm.
* User: code
* Date: 2018/3/19
* Time: 下午4:55
*/
namespace App\Models;
use App\Repositories\Common\Traits\LaravelModelEmptyTimestampTrait;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\DB;
/**
* Class Model
*
* // $timestamps为取消时间自动更新
*
* @method \Illuminate\Contracts\Pagination\LengthAwarePaginator paginateRaw($query, $perPage = 15, $countRaw = 'count(*) as aggregate', $columns = ['*'], $pageName = 'page', $page = null)
* @method static $this|Builder|QueryBuilder whereRange($field, $min, $max, $eq = '=')
* @method static $this|Builder|QueryBuilder havingRange($field, $min, $max, $eq = '=')
* @method static $this|Builder|QueryBuilder where($column, $operator = null, $value = null, $boolean = 'and')
* @method static $this|Builder|QueryBuilder whereIn($column, $values, $boolean = 'and', $not = false)
* @method static mixed insert(array $values) // 看\Illuminate\Database\Query\Builder::insert
* @method static QueryBuilder from($table)
* @method static $this|Builder|QueryBuilder orderBy($column, $direction = 'asc')
* @method static $this|Builder|QueryBuilder updateOrCreate(array $attributes, array $values = []) 更新或者创建,$attributes为where条件
* @method static $this|Builder|QueryBuilder firstOrNew(array $attributes)
* @method static $this|mixed|QueryBuilder find($id, $columns = ['*'])
* @package App\Models
* @since \Illuminate\Database\Eloquent\Builder
*/
class Model extends \Illuminate\Database\Eloquent\Model
{
use LaravelModelEmptyTimestampTrait;
/**
* false取消更新created_at和updated_at
*
* @var bool
*/
public $timestamps = true;
/**
* 通过app获取单例方法
*
* @return $this|Builder|Builder
*/
public static function instance()
{
return app(static::class);
}
/**
* 事务
*
* @param mixed ...$args
* @return mixed
*/
public static function transaction(...$args)
{
return DB::connection(static::instance()->connection)->transaction(...$args);
}
/**
* where区间搜索,可单条件过滤
*
* @param Builder|QueryBuilder $query
* @param $field
* @param $min
* @param $max
* @param string $eq
* @return Builder|QueryBuilder
*/
public function scopeWhereRange($query, $field, $min, $max, $eq = '=')
{
if (!empty($min) && !empty($max) && $min > $max){ // 所有
return $query;
}
if (!empty($min)) {
$query->where($field, '>' . $eq, $min);
}
if (!empty($max)) {
$query->where($field, '<' . $eq, $max);
}
return $query;
}
/**
* where区间搜索,可单条件过滤
*
* @param Builder|QueryBuilder $query
* @param $field
* @param $min
* @param $max
* @param string $eq
* @return Builder|QueryBuilder
*/
public function scopeHavingRange($query, $field, $min, $max, $eq = '=')
{
if ($min > $max){ // 所有
return $query;
}
if (!empty($min)) {
$query->having($field, '>' . $eq, $min);
}
if (!empty($max)) {
$query->having($field, '<' . $eq, $max);
}
return $query;
}
/**
* 分页,和系统paginate一样,增加$countRaw可以控制count的字段
*
* @param Builder|QueryBuilder $query
* @param int $perPage
* @param string $countRaw count统计字段默认aggregate别名
* @param array $columns
* @param string $pageName
* @param int|null $page
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
*/
public function scopePaginateRaw($query, $perPage = 15, $countRaw = 'count(*) as aggregate', $columns = ['*'], $pageName = 'page', $page = null)
{
$page = $page ?: Paginator::resolveCurrentPage($pageName);
// count统计
/** @var Builder $total */
$total = (clone $query)->select([])->forPage(1, 1)->selectRaw($countRaw);
$total->getQuery()->orders = null; // 清空数据
$total = $total->first();
$total = intval($total['aggregate'] ?? 0); // 默认aggregate别名
$results = $total ? $query->forPage($page, $perPage)->get($columns) : [];
// 这个对象可以直接foreach,拿到model
return new LengthAwarePaginator($results, $total, $perPage, $page, [
'path' => Paginator::resolveCurrentPath(),
'pageName' => $pageName,
]);
}
/**
* @inheritdoc
*/
protected function castAttribute($key, $value)
{
if (is_null($value)) {
return $value;
}
// 处理空时间
$temp = $this->castAttributeByTimestampTrait($key, $value);
if ($temp !== false){
return $temp;
}
switch ($this->getCastType($key)) {
case 'pic': // 图片,系统默认七牛
return getImgUrlPath($value);
}
return parent::castAttribute($key, $value);
}
}
<?php
/**
* 兼容empty,支持对象
*
* @param $data
* @return bool
*/
if (!function_exists('emptyA')) {
function emptyA($data)
{
if (is_object($data) && is_a($data, \Illuminate\Support\Collection::class)) {
/** @var \Illuminate\Support\Collection $data */
return $data->isEmpty();
}
return empty($data);
}
}
if (!function_exists('ifNotProduction')){
/**
* 判断不是生产环境
*
* @param null $true
* @param null $false
* @return bool
*/
function ifNotProduction($true = null, $false = null){
$bool = config('app.env') !== 'production';
if ($bool && !is_null($true)){
return $true;
}else if (!$bool && !is_null($false)){
return $false;
}
return $bool;
}
}
if (!function_exists('getStorageConfig')) {
/**
* 获取系统存储文件的配置
*
* @param string $get 配置的key
* @param null|string $disk
* @return bool
*/
function getStorageConfig($get, $disk = null)
{
return \Illuminate\Support\Facades\Storage::disk($disk)->getDriver()->getConfig()->get($get);
}
}
if (!function_exists('ifApiMiddlewareRequest')) {
/**
* 可用于是否api请求
* 根据是否放在api中间件中
*
* @param null $request
* @param string $middlewareName
* @return bool
*/
function ifApiMiddlewareRequest($request = null, $middlewareName = 'api'){
$route = ($request ?? request())->route();
return in_array($middlewareName, $route ? $route->gatherMiddleware() : []);
}
}
<?php
/**
* Created by PhpStorm.
* User: code
* Date: 2018/10/8
* Time: 下午7:45
*/
namespace App\Repositories\Common\Traits;
use Illuminate\Database\Eloquent\Model;
/**
* 解决laravel中model对timestamp字段报错的问题
* 如果timestamp字段为not null default也依然报错
* 同时解决值为0000-00-00 00:00:00的字段显示
*
* Trait LaravelModelTimestampTrait
* @package App\Repositories\Common\Traits
*/
trait LaravelModelEmptyTimestampTrait
{
protected static $emptyTimestamp = '0000-00-00 00:00:00';
/**
* 入口方法
*
*
* @example 需在model中定义
protected $casts = [
'start_time' => 'empty_timestamp',
'end_time' => 'empty_timestamp',
];
*/
protected static function bootLaravelModelEmptyTimestampTrait()
{
static::creating(function ($model){
/** @var Model $model */
if (static::ifFinishByTimestampSetTrait($model)){
$casts = $model->getCasts();
foreach ($casts as $field => $cast) {
if ($cast === 'empty_timestamp' && empty($model[$field])) {
$model[$field] = static::$emptyTimestamp;
}
}
}
});
}
/**
* 是否完成定义
*
* @param Model $model
* @return bool
*/
protected static function ifFinishByTimestampSetTrait($model)
{
return !empty($model->getCasts()) && in_array('empty_timestamp', $model->getCasts());
}
/**
* 判断是否空时间
*
* @param $value
* @return bool
*/
protected function ifEmptyTimestamp($value)
{
if (empty($value)){
return true;
}else if (is_string($value) && ($value === static::$emptyTimestamp || $value === $this->serializeDate(date_create(static::$emptyTimestamp)))){ // 字符串
return true;
}else if ($value instanceof \DateTime && $value->getTimestamp() === date_create(static::$emptyTimestamp)->getTimestamp()){
return true;
}
return false;
}
/**
* 需重写castAttribute方法,在内部调用
*
* @example
protected function castAttribute($key, $value)
{
if (is_null($value)) {
return $value;
}
// 处理空时间
$temp = $this->castAttributeByTimestamp($key, $value);
if ($temp !== false){
return $temp;
}
return parent::castAttribute($key, $value);
}
*
* @param $key
* @param $value
* @return false|string
*/
protected function castAttributeByTimestampTrait($key, $value)
{
/** @var Model|$this $this */
if (!$this->ifFinishByTimestampSetTrait($this)){
return false;
}
switch ($this->getCastType($key)) {
case 'empty_timestamp': // 能处理空时间
if ($this->ifEmptyTimestamp($value)) {
return '';
}
// 使用原有方法
return $this->fromDateTime($value);
}
return false;
}
}
<?php
/**
* Created by PhpStorm.
* User: code
* Date: 2018/10/17
* Time: 下午5:40
*/
namespace App\Repositories\Common\Traits;
use App\Models\Model;
use App\Repositories\Common\FuncMiddleware;
trait SingleApiControllerTrait
{
/**
* 获取列表
*
* @param string $modelName
* @param array $field
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
*/
protected function simplePageTrait($modelName, $field = null)
{
$this->validatorAll([
'total' => 'integer|min:0',
]);
$total = request()->input('total');
/** @var Model $modelClass */
$modelClass = app($modelName);
return $modelClass->where([])->select(...argsSaveDefault($field))->paginate($total);
}
/**
* 根据id获取详情
*
* @param string $modelName
* @param array $field
* @return \Illuminate\Database\Eloquent\Model|mixed|null|static
*/
protected function detailByIdTrait($modelName, $field = null)
{
$this->validatorAll([
'id' => 'required|integer|min:1',
]);
$id = request()->input('id');
/** @var Model $modelClass */
$modelClass = app($modelName);
$data = $modelClass->where('id', $id)->first(...argsSaveDefault($field));
if (emptyA($data)){ // 空数据
$this->throwJson($this->success());
};
return $data;
}
/**
* 根据$where执行updateOrCreate
*
* $this->updateOrCreateTrait(DemoModel::class, [
* 'staff_id' => Auth::id(),
* ], [
* 'name' => 'required|string|max:20',
* 'first_text' => 'required|string',
* 'voice_text' => 'required|string',
* ]);
*
* @param $modelName
* @param $where
* @param array $rule
* @param array $allowField
* @return Model|bool
*/
protected function updateOrCreateTrait($modelName, $where, $rule = [], $allowField = [])
{
$request = request();
if (!$request->isMethod('POST')) {
$this->throwJson($this->error(trans('messages.request-error')));
}
$this->validatorAll($rule);
$data = $request->request->all();
if (!empty($allowField)) {
$data = array_intersect_key($data, array_flip($allowField));
}
return FuncMiddleware::instance()->call('updateOrCreateTrait', function ($data)use($modelName, $where){
/** @var Model $modelClass */
$modelClass = app($modelName);
/** @var Model $modelData */
return $modelClass->updateOrCreate($where, $data);
}, $data);
}
/**
* 根据id更新数据
* create返回model
* update返回bool
*
* @param $modelName
* @param array $rule
* @param array $allowField
* @param array $createRule 创建时的规则
* @return Model|bool
*/
protected function updateByIdTrait($modelName, $rule = [], $allowField = [], $createRule = [])
{
$request = request();
if (!$request->isMethod('POST')) {
$this->throwJson($this->error(trans('messages.request-error')));
}
$this->validatorAll([
'id' => ['sometimes', 'required', 'integer'],
]);
$this->validatorAll($rule);
$id = $request->input('id');
$data = $request->request->all();
if (!empty($allowField)) {
$data = array_intersect_key($data, array_flip($allowField));
}
/** @var Model $modelClass */
$modelClass = app($modelName);
if (empty($id)) { // 创建成功后需要返回
$this->validatorAll($createRule); // 会自动抛出
return FuncMiddleware::instance()->call('updateByIdTrait.create', function ($data)use($modelClass, $id){
/** @var Model $modelData */
return $modelClass->create($data);
}, $data);
}else{
$data['id'] = $id; // 补充回id
$modelData = $modelClass->find($id);
if (emptyA($modelData)) { // 不存在的数据
$this->throwJson($this->error(trans('messages.request-error')));
}
return FuncMiddleware::instance()->call('updateByIdTrait.update', function ($data, $modelData)use($id){
/** @var Model $modelData */
return $modelData->where('id', $id)->update($data);
}, $data, $modelData);
}
}
/**
* 封装上面方法
* 用于保证创建的唯一性
* 限制唯一条件$unqiueField
*
* @param string $unqiueField
* @inheritdoc
*/
protected function updateAtUniqueByIdTrait($modelName, $unqiueField, $rule = [], $allowField = [])
{
return $this->updateByIdTrait($modelName, $rule, $allowField, [
$unqiueField => $this->uniqueRuleByModelName($modelName, $unqiueField),
]);
}
/**
* 唯一字段的规则
*
* @param string $modelName
* @param string $unqiueField
* @return string
*/
protected function uniqueRuleByModelName($modelName, $unqiueField)
{
/** @var Model $modelClass */
$modelClass = app($modelName);
return 'unique:' . join('.', array_filter([$modelClass->getConnectionName(), $modelClass->getTable()])) . ',' . $unqiueField;
}
/**
* 根据id删除
*
* @param $modelName
* @return bool|int|mixed|null
* @throws \Exception
*/
protected function deleteByIdTrait($modelName)
{
$this->validatorAll([
'id' => 'integer|min:0',
]);
$id = request()->input('id');
/** @var Model $modelClass */
$modelClass = app($modelName);
return $modelClass->where('id', $id)->delete();
}
}
<?php
/**
* Created by PhpStorm.
* User: code
* Date: 2018-12-19
* Time: 17:38
*/
namespace App\Repositories\Common\Traits\Tinyint;
use App\Repositories\Common\Traits\SysTinyintTrait;
/**
* laravel专用的TinyintModelTrait
*/
trait LaravelSysTinyintTrait
{
use SysTinyintTrait;
/**
* 移除缓存,通过在tinyint中定义
*
* @param $data
*/
public function forgetActionCache($data)
{
$this->getTinyint('callback')->getListByCallback('forget', $data);
}
}
<?php
/**
* Created by PhpStorm.
* User: code
* Date: 2018-12-20
* Time: 11:24
*/
namespace App\Console\Commands;
use App\Listeners\SaveSqlListener;
/**
* 系统命令类库,实现多方法
*/
class Command extends \Illuminate\Console\Command
{
protected $description = '系统命令类库,实现多方法';
protected $signature = 'command:helper';
public function __construct()
{
$this->signature .= ' {method? : 执行的方法}';
$this->signature .= ' {--arg=* : 方法的额外参数}';
$this->signature .= ' {--s|sql : 打印sql}'; // -s或者--sql
parent::__construct();
}
/**
* 执行入口
*/
public function handle()
{
$args = $this->option('arg'); // 数组
$method = $this->argument('method');
if (empty($method)) { // 必传
$this->error('请指定执行的方法!');
return;
}
/**
* 开启sql记录
*/
// 打印sql,需要额外记录类库
if ($this->option('sql') && class_exists(SaveSqlListener::class)) {
SaveSqlListener::setAutoSendCallback(function ($data){
$this->info(json_encode($data));
});
SaveSqlListener::setCloseBool(false);
}
call_user_func_array([$this, $method], $args);
}
}
<?php
namespace App\Listeners;
use Illuminate\Database\Events\QueryExecuted;
/**
* 保存执行的sql语句
* 修改文件:app/Providers/EventServiceProvider.php
*
* @example 使用:
protected $listen = [
\Illuminate\Database\Events\QueryExecuted::class => [
\App\Listeners\SaveSqlListener::class
],
];
public function boot()
{
\App\Listeners\SaveSqlListener::setCacheBool();
\App\Listeners\SaveSqlListener::saveFile(); // 本地
parent::boot();
//
}
*
*/
class SaveSqlListener
{
/**
* 手动开启
*
* @var bool
*/
protected static $closeBool = true;
/**
* 是否直接输出
*
* @var callable[]|\Closure[]
*/
protected static $autoSendCallback = [];
/**
* 对象缓存
*
* @var array
*/
protected static $cache = [];
/**
* 是否开启对象缓存
*
* @var bool
*/
protected static $cacheBool = false;
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* @return array
*/
public static function getCache(): array
{
return static::$cache;
}
/**
* @param bool $closeBool
*/
public static function setCloseBool(bool $closeBool)
{
static::$closeBool = $closeBool;
}
/**
* @param callable $autoSendCallback
*/
public static function setAutoSendCallback(callable $autoSendCallback)
{
static::$autoSendCallback = [];
static::addAutoSendCallback($autoSendCallback);
}
public static function addAutoSendCallback(callable $autoSendCallback)
{
if (!is_callable($autoSendCallback)){
return;
}
static::$autoSendCallback[] = $autoSendCallback;
}
/**
* 指定保存到文件
* @param string $logPath
*/
public static function saveFile($logPath = 'logs/sql.log')
{
static::addAutoSendCallback(function ($data) use($logPath){
file_put_contents(storage_path($logPath), str_repeat('=', 40) . PHP_EOL, FILE_APPEND);
file_put_contents(storage_path($logPath), var_export($data, true) . PHP_EOL, FILE_APPEND);
});
}
/**
* @param bool $cacheBool
*/
public static function setCacheBool(bool $cacheBool = true): void
{
self::$cacheBool = $cacheBool;
}
/**
* Handle the event.
*
* @param QueryExecuted $event
* @return void
*/
public function handle(QueryExecuted $event)
{
if (config('app.env') == 'production' && self::$closeBool) { // 线上环境不记录
return;
}
$data = [
'sql' => $event->sql,
'bindings' => $event->bindings,
'time' => $event->time,
];
if (!empty(static::$autoSendCallback)){
foreach (static::$autoSendCallback as $item) {
call_user_func($item, $data);
}
}
if (static::$cacheBool){
static::$cache[] = $data;
}
}
}
<?php
/**
* Created by PhpStorm.
* User: code
* Date: 2018-12-25
* Time: 10:35
*/
namespace App\Http\Middleware;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
/**
* http的cache,用于http的缓存
*/
class HttpCacheMiddleware
{
/**
*
* @param Request $request
* @param \Closure $next
* @param int $time 秒
* @param bool|int $beforeBool 在执行前检测
* @param bool $userBool 是否做用户区分
* @return mixed
*/
public function handle($request, \Closure $next, $time, $beforeBool = 0, $userBool = false)
{
$response = Response::create();
// 只做客户端的检测
if ($beforeBool && $this->handleResponse($response, $request, $time, $userBool)){
return $response; // 304成功
}
/** @var Response $response */
$response = $next($request); // 正常执行代码
$this->handleResponse($response, $request, $time, $userBool);
return $response;
}
/**
* @param Response $response
* @param Request $request
* @param int $time
* @param $userBool
* @return bool
*/
protected function handleResponse($response, $request, $time, $userBool)
{
$response->setEtag($this->getUrlEtag($request, $userBool))->setMaxAge($time)->setSharedMaxAge($time);
if (!$response->getLastModified()) { // 没有设置,就用客户端的
$time = strtotime($request->header('If-Modified-Since', ''));
$date = date_create();
if ($time){
$date->setTimestamp($time);
}
$response->setLastModified($date);
}
return $response->isNotModified($request);
}
/**
* @param Request $request
* @param $userBool
* @return string
*/
protected function getUrlEtag($request, $userBool)
{
$string = $request->fullUrl();
if ($userBool){
$string .= Auth::id();
}
return md5($string);
}
}
<?php
/**
* Created by PhpStorm.
* User: code
* Date: 2018-12-26
* Time: 16:33
*/
namespace App\Repositories\Common;
/**
* laravel扩展七牛
*/
class QiNiuHelpers
{
use Traits\SysSingleton;
/**
* @var QiNiu\BucketManager
*/
protected $sysBucketManager;
/**
* @var string|null
*/
protected $disk;
/**
* v2版的fetch
*
* 但任务没有一次成功过,原因未知
*
* @param $url
* @param $key
* @return bool|mixed
*/
public function fetchV2($url, $key)
{
return $this->getSysBucketManager()->fetchV2($url, $this->getBucket(), $key);
}
/**
* 执行fetch并返回文件地址
*
* @param $url
* @param $key
* @return bool|string
*/
public function getFetchV2Key($url, $key)
{
$response = $this->fetchV2($url, $key);
if ($response === false){
return false;
}
return $this->getDisk()->getUrl($key);
}
/**
* 执行fetch并返回文件地址
*
* @param $fetchId
* @return bool|string
*/
public function getFetchNewStatus($fetchId)
{
$response = $this->getSysBucketManager()->fetchV2Status($fetchId);;
if ($response === false){
return false;
}
return $response;
}
/**
* @return \Illuminate\Filesystem\FilesystemAdapter|\Overtrue\Flysystem\Qiniu\QiniuAdapter|\League\Flysystem\Filesystem|mixed
*/
public function getDisk()
{
return \Illuminate\Support\Facades\Storage::disk($this->disk);
}
/**
* 获取七牛的sdk的bucketManager类
*
* @return \Qiniu\Storage\BucketManager
*/
public function getBucketManager()
{
// composer加载了overtrue/flysystem-qiniu
return $this->getQiNiuAdapter()->getBucketManager();
}
/**
* @return \League\Flysystem\AdapterInterface|\Overtrue\Flysystem\Qiniu\QiniuAdapter
*/
public function getQiNiuAdapter()
{
return $this->getDisk()->getAdapter();
}
/**
* 获取bucket
*
* @return string
*/
public function getBucket()
{
$bucket = $this->getDisk()->getConfig()->get('bucket');
if (empty($bucket)) { // 通常都没有值,还是要config读取
return config("filesystems.disks.{$this->disk}.bucket", '');
}
return '';
}
/**
* @return QiNiu\BucketManager
*/
public function getSysBucketManager()
{
if (is_null($this->sysBucketManager)) {
$this->sysBucketManager = new QiNiu\BucketManager();
$this->sysBucketManager->setAuth($this->getQiNiuAdapter()->getAuthManager());
}
return $this->sysBucketManager;
}
/**
* @param string|null $disk
* @return $this
*/
public function setDisk(?string $disk)
{
$this->disk = $disk;
return $this;
}
}
<?php
namespace App\Exceptions;
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler
{
use \Flugg\Responder\Exceptions\ConvertsExceptions; // 使用flugger/laravel-responder扩展
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
//
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];
/**
* Report or log an exception.
*
* @param \Exception $exception
* @return void
*/
public function report(Exception $exception)
{
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
static $convertBool = false; // 是否经过转换
$apiExceptionArr = [
// jwt自带中间件报错
\Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException::class => \Flugg\Responder\Exceptions\Http\UnauthenticatedException::class,
];
if ($convertBool || $request->wantsJson() || array_key_exists(get_class($exception), $apiExceptionArr)) {
$convertBool = true;
// 转换异常
$this->convert($exception, $apiExceptionArr);
$this->convertDefaultException($exception);
if ($exception instanceof \Flugg\Responder\Exceptions\Http\HttpException) {
return $this->renderResponse($exception);
}
}
return parent::render($request, $exception);
}
}
<?php
/**
* Created by PhpStorm.
* User: aozhuochao
* Date: 2018-12-13
* Time: 11:04
*/
namespace App\Helper\Traits;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* 通过scope给where扩展
*
* @method Builder|QueryBuilder|$this ifWhere($bool, $column, $operator = null, $value = null, $boolean = 'and')
* @method Builder|QueryBuilder|$this ifWhereBetween($bool, $column, array $values, $boolean = 'and', $not = false)
* @method Builder|QueryBuilder|$this ifWith($bool, $relations)
* @method static $this|Builder|QueryBuilder whereRange($field, $min = '', $max = '', $eq = '=')
* @method static $this|Builder|QueryBuilder havingRange($field, $min = '', $max = '', $eq = '=')
* @method \Illuminate\Contracts\Pagination\LengthAwarePaginator paginateRaw($query, $perPage = 15, $countRaw = 'count(*) as aggregate', $columns = ['*'], $pageName = 'page', $page = null)
*/
trait ModelScopeWhereTrait
{
/**
* ifWhere,如果$bool为真就执行where条件
*
* @param Builder $query
* @param bool $bool 是否where
* @param array $whereArgs
* @return Builder|QueryBuilder|$this
*/
public function scopeIfWhere($query, $bool, ...$whereArgs)
{
if ($bool && !empty($whereArgs)){
$query->where(...$whereArgs);
}
return $query;
}
/**
* ifWhereBetween,如果$bool为真就执行whereBetween条件
*
* @param Builder $query
* @param bool $bool 是否whereBetween
* @param array $whereArgs
* @return Builder|QueryBuilder|$this
*/
public function scopeIfWhereBetween($query, $bool, ...$whereArgs)
{
if ($bool && !empty($whereArgs)){
$query->whereBetween(...$whereArgs);
}
return $query;
}
/**
* ifWith,如果$bool为真就执行with
*
* @param Builder $query
* @param bool $bool 是否使用
* @param array ...$withArgs
* @return Builder|QueryBuilder|$this
*/
public function scopeIfWith($query, $bool, ...$withArgs)
{
if ($bool && !empty($withArgs)){
$query->with(...$withArgs);
}
return $query;
}
/**
* where区间搜索,可单条件过滤
*
* @param Builder|QueryBuilder $query
* @param $field
* @param $min
* @param $max
* @param string $eq
* @return Builder|QueryBuilder
*/
public function scopeWhereRange($query, $field, $min = '', $max = '', $eq = '=')
{
if (!empty($min) && !empty($max) && $min > $max){ // 所有
return $query;
}
if (!empty($min) && !empty($max)){ // 指定两个
$query->whereBetween($field, [$min, $max]);
return $query;
}
if (!empty($min)) {
$query->where($field, '>' . $eq, $min);
}
if (!empty($max)) {
$query->where($field, '<' . $eq, $max);
}
return $query;
}
/**
* where区间搜索,可单条件过滤
*
* @param Builder|QueryBuilder $query
* @param $field
* @param $min
* @param $max
* @param string $eq
* @return Builder|QueryBuilder
*/
public function scopeHavingRange($query, $field, $min = '', $max = '', $eq = '=')
{
if ($min > $max){ // 所有
return $query;
}
if (!empty($min) && !empty($max)){ // 指定两个
$query->havingBetween($field, [$min, $max]);
return $query;
}
if (!empty($min)) {
$query->having($field, '>' . $eq, $min);
}
if (!empty($max)) {
$query->having($field, '<' . $eq, $max);
}
return $query;
}
/**
* 分页,和系统paginate一样,增加$countRaw可以控制count的字段
*
* @param Builder|QueryBuilder $query
* @param int $perPage
* @param string $countRaw count统计字段默认aggregate别名
* @param array $columns
* @param string $pageName
* @param int|null $page
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
*/
public function scopePaginateRaw($query, $perPage = 15, $countRaw = 'count(*) as aggregate', $columns = ['*'], $pageName = 'page', $page = null)
{
$page = $page ?: Paginator::resolveCurrentPage($pageName);
// count统计
/** @var Builder $total */
$total = (clone $query)->select([])->forPage(1, 1)->selectRaw($countRaw);
$total->getQuery()->orders = null; // 清空数据
$total = $total->first();
$total = intval($total['aggregate'] ?? 0); // 默认aggregate别名
$results = $total ? $query->forPage($page, $perPage)->get($columns) : [];
// 这个对象可以直接foreach,拿到model
return new LengthAwarePaginator($results, $total, $perPage, $page, [
'path' => Paginator::resolveCurrentPath(),
'pageName' => $pageName,
]);
}
}
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
/**
* 数值或者数值数组
*/
class NumberOrArrayNumber implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
if (is_numeric($value)) {
return true;
}else if(is_array($value)){
foreach ($value as $item) {
if (!is_numeric($item)) {
return false;
}
}
return true;
}
return false;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return ':attribute不是数值';
}
}
<?php
namespace App\Library\Traits;
use Illuminate\Support\Str;
/**
* 处理model类名
* 如:
* 1、支持model类名后面带Model的情况,下的hasMany自动拼接字段名
*/
trait HandleModelClassNameAtRelationTrait
{
public function getForeignKey()
{
return Str::snake(preg_replace("/Model$/", "", class_basename($this))).'_'.$this->getKeyName();
}
}
<?php
/**
* User: aozhuochao
* Date: 2019/8/31
*/
namespace App\Library\Vendor\Laravel;
/**
* Class DB
* @package App\Library\Vendor\Laravel
* @mixin \Illuminate\Database\DatabaseManager
* @mixin \Illuminate\Database\Connection
*/
class DB extends \Illuminate\Support\Facades\DB
{
}
以上是关于php laravel类库相关的主要内容,如果未能解决你的问题,请参考以下文章