laravel 开发辅助工具
laravel 开发辅助工具
配置
添加服务提供商
将下面这行添加至 config/app.php 文件 providers 数组中:
'providers' => [
...
App\Plugins\Auth\Providers\LaravelServiceProvider::class
]
插件及文档
Repository 模式
插件介绍
首先需要声明的是设计模式和使用的框架以及语言是无关的,关键是要理解设计模式背后的原则,这样才能不管你用的是什么技术,都能够在实践中实现相应的设计模式。
按照最初提出者的介绍,Repository 是衔接数据映射层和领域层之间的一个纽带,作用相当于一个在内存中的域对象集合。客户端对象把查询的一些实体进行组合,并把它 们提交给 Repository。对象能够从 Repository 中移除或者添加,就好比这些对象在一个 Collection 对象上进行数据操作,同时映射层的代码会对应的从数据库中取出相应的数据。
从概念上讲,Repository 是把一个数据存储区的数据给封装成对象的集合并提供了对这些集合的操作。
Repository 模式将业务逻辑和数据访问分离开,两者之间通过 Repository 接口进行通信,通俗点说,可以把 Repository 看做仓库管理员,我们要从仓库取东西(业务逻辑),只需要找管理员要就是了(Repository),不需要自己去找(数据访问),具体流程如下图所示:
创建 Repository
不使用缓存
php artisan make:repo User
使用缓存
php artisan make:repo User --cache
创建 UserRepository 时会询问是否创建Model ,如果Model以存在,需要把 AppRepositoriesModulesUserProvider::class 的Model替换成当前使用的Model
配置Providers
将下面这行添加至 AppProvidersAppServiceProvider::class 文件 register 方法中:
public function register()
{
$this->app->register(\App\Repositories\Modules\User\Provider::class);
}
使用
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Repositories\Modules\User\Interfaces;
class HomeController extends Controller
{
protected $repo = null;
public function __construct(Interfaces $repo)
{
$this->repo = $repo;
}
public function index(Request $request){
return $this->respondWithSuccess($this->repo->get(['*']));
}
}
配合 Search 更灵活
public function index(Request $request){
return $this->respondWithSuccess(
$this->repo->getwhere(
new IndexSearch($request->olny(['name'])) ,
['*']
)
);
}
方法
表单搜索辅助插件
插件介绍
把表单提交的一些参数传换成 where 语句.
创建 Search
生成一个UserController::index控制器使用的搜索辅助类
php artisan make:search User\IndexSearch
上面命令会创建一个 AppSearchsModulesUserIndexSearch::class 的类
创建Search时,建议根据 ControllerActionSearch 的格式创建。
编写Search
<?php
namespace App\Searchs\Modules\User;
use luffyzhao\laravelTools\Searchs\Facades\SearchAbstract;
class IndexSearch extends SearchAbstract
{
protected $relationship = [
'phone' => '=',
'name' => 'like',
'date' => 'between'
];
public function getNameAttribute($value)
{
return $value . '%';
}
public function getDateAttribute($value){
return function ($query){
$query->where('date', '>', '2018-05-05')->where('status', 1);
};
}
}
使用Search
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Repositories\Modules\User\Interfaces;
use App\Searchs\Modules\User\IndexSearch;
class HomeController extends Controller
{
protected $repo = null;
public function __construct(Interfaces $repo)
{
$this->repo = $repo;
}
public function index(Request $request){
return $this->respondWithSuccess(
$this->repo->getWhere(
new IndexSearch(
$request->only(['phone', 'name', 'date'])
),
['*']
)
);
}
}
生成的sql
请求参数:
phone=18565215214&name=成龙&date=2018-08-21
生成的sql
WHERE (phone = 18565215214) AND (name like '成龙%') AND (date > '2018-05-05' AND status = 1)
```
Excels导出辅助插件
插件介绍
Excels导出辅助插件
创建 Excels
php artisan make:excel User
上面命令会创建一个 AppExcelsModulesUserExcel::class 的类
编写Search
<?php
namespace App\Excels\Modules;
use App\Excels\Facades\ExcelAbstract;
use App\Repositories\Modules\User\Interfaces;
use App\Searchs\Modules\User\ExcelSearch;
class CarExcel extends ExcelAbstract
{
public function __construct(Interfaces $repo)
{
parent::__construct($repo);
}
/**
* Excel标题列
* @return {[type]} [description]
*/
public function headings()
{
return ['ID','手机号码','姓名'];
}
/**
* @param mixed $row
*
* @return array
*/
public function map($row)
{
return [
$row->id,
$this->phone,
$this->name
];
}
/**
* 搜索参数
* @return {[type]} [description]
*/
protected function getAttributes()
{
return new ExcelSearch(request()->only([
'phone',
'name',
]));
}
}
更多用法 请参考 maatwebsite/excel
Sql 写进日志-事件
介绍
把sql语句记录到日志里
使用
在 laravel 自带的 EventServiceProvider 类里 listen 添加
'Illuminate\Database\Events' => [
'luffyzhao\laravelTools\Listeners\QueryListeners'
]
生成事件
php artisan event:generate
Controller Traits
介绍
controller公用方法
使用方法
在 AppHttpControllersController 类中 use luffyzhaolaravelToolsTraitsResponseTrait
Sign 加签
插件介绍
请求参数加签验证
配置 Sign
如果你使用的是md5加签方式请在config/app.php文件中,添加 sign_key 配置。如果你使用的是Rsa加签方式请在config/app.php文件中,添加app.sign_rsa_private_key和app.sign_rsa_public_key配置
配置中间件
在app/Http/Kernel.php文件中,您需要把 'sign' => luffyzhaolaravelToolsMiddlewareVerifySign::class, 添加到$routeMiddleware属性中
使用
<?php
Route::group(
['middleware' => 'sign:api'],
function($route){
Route::get('xxx', 'xxx');
}
);
加签方式
rsa 和 md5
参数排序
- 准备参数
- 添加
timestamp 字段
- 然后按照字段名的 ASCII 码从小到大排序(字典序)
- 生成
url 参数串
- 拼接 key 然后 md5 或者 rsa
如下所示:
{
"name": "4sd65f4asd5f4as5df",
"aimncm": "54854185",
"df4": ["dfadsf"],
"dfsd3": {
"a": {
"gfdfsg": "56fdg",
"afdfsg": "56fdg"
}
}
}
排序后:
{
"aimncm": "54854185",
"df4": ["dfadsf"],
"dfsd3": {
"a": {
"afdfsg": "56fdg",
"gfdfsg": "56fdg"
}
},
"name": "4sd65f4asd5f4as5df",
"timestamp": "2018-05-29 17:25:34"
}
生成url参数串:
aimncm=54854185&df4[0]=dfadsf&dfsd3a=56fdg&dfsd3a=56fdg&name=4sd65f4asd5f4as5df×tamp=2018-05-29 17:25:34
拼接 key :
aimncm=54854185&df4[0]=dfadsf&dfsd3a=56fdg&dfsd3a=56fdg&name=4sd65f4asd5f4as5df×tamp=2018-05-29 17:25:34base64:Z9I7IMHdO+T9qD3pS492GWNxNkzCxinuI+ih4xC4dWY=
md5加密
ddab78e7edfe56594e2776d892589a9c
redis-token 认证
插件介绍
把token保存在redis。同时支持登录过期时间设置,登录之前,登录之后事件处理。
配置 Auth guard
在 config/auth.php 文件中,你需要将 guards/driver 更新为 redis-token:
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
...
'guards' => [
'api' => [
'driver' => 'redis-token',
'provider' => 'users',
],
],
更改 Model
如果需要使用 redis-token 作为用户认证,我们需要对我们的 User 模型进行一点小小的改变,实现一个接口,变更后的 User 模型如下:
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use luffyzhao\laravelTools\Auths\Redis\RedisTokeSubject;
class User extends Authenticatable implements RedisTokeSubject
{
public function getIdentifier(){
return $this->getKey();
}
}
登录
/**
* 登录
* @method store
* @param StoreRequest $request
*
* @return \Illuminate\Http\JsonResponse
*
* @author luffyzhao@vip.126.com
*/
public function store(StoreRequest $request)
{
$token = auth('api')->attempt(
$request->only(['phone', 'password'])
);
if (!$token) {
return $this->respondWithError('用户不存在,或者密码不正确!');
}
return $this->respondWithToken((string) $token);
}
退出
/**
* 退出登录.
*
* @method logout
*
* @return \Illuminate\Http\JsonResponse
*
* @author luffyzhao@vip.126.com
*/
public function logout()
{
auth('api')->logout();
return $this->respondWithSuccess([], '退出成功');
}
事件
方法
原文地址:https://segmentfault.com/a/1190000015903727
laravel 开发辅助工具的更多相关文章
- Bytom Kit开发辅助工具介绍
Bytom Kit是一款为了帮助开发者更简单地理解Bytom的开发辅助工具,集合了校验.标注.解码.测试水龙头等功能. 该工具用python语言封装了一套比原的API和7个工具方法,如果有开发需求可以 ...
- Shellcode开发辅助工具shellnoob
Shellcode开发辅助工具shellnoob Shellcode开发的过程中会遇到很多繁杂的工作,如编译.反编译.调试等.为了减少这部分工作,Kali Linux提供了开发辅助工具shelln ...
- 如何精准高效的实现视觉稿?------前端开发辅助工具AlloyDesigner使用介绍
AlloyDesigner:http://alloyteam.github.io/AlloyDesigner/ 介绍:AlloyDesigner是腾讯开发的一款工具,其在页面构建过程中,直接嵌入开发的 ...
- nodejs开发辅助工具nodemon
前面的话 修改代码后,需要重新启动 Express 应用,所做的修改才能生效.若之后的每次代码修改都要重复这样的操作,势必会影响开发效率,本文将详细介绍Nodemon,它会监测项目中的所有文件,一旦发 ...
- 不错的 iOS 开发辅助工具
一,常用 1> iPhone 日志插件iConsole.
- 制作ado开发辅助工具类SqlHelper
public static class SqlHelper { //通过配置文件获取连接字符创 private static readonly string constr = Configuratio ...
- .NET开发辅助工具-ANTS Performance Profiler【转载】
https://blog.csdn.net/Eye_cng/article/details/50274109
- Windows开发中一些常用的辅助工具
经常有人问如何快速的定位和解决问题,很多时候答案就是借助工具, 记录个人Windows开发中个人常用的一些辅助工具. (1) Spy++ 相信windows开发中应该没人不知道这个工具, 我们常用 ...
- 翻译:Laravel-4-Generators 使用自己定义代码生成工具高速进行Laravel开发
使用自己定义代码生成工具高速进行Laravel开发 这个Laravle包提供了一种代码生成器,使得你能够加速你的开发进程.这些生成器包含: generate:model – 模型生成器 generat ...
随机推荐
- P4460 [CQOI2018]解锁屏幕
算是我比较擅长的类型,自己想想就会了.普通小状压,状态傻子都能想出来.一开始裸的枚举T了,30.后来与处理之后跑的飞起,就是不对,还是30分.后来看讨论版...mod竟然是1e8+7!!!这不有毒吗. ...
- bzoj3786
splay维护dfs序 我们发现有移动子树这种操作,树剖是做不了了,又要实现子树加,lct又维护不了了,这时我们用splay维护入栈出栈序来支持这些操作.我们记录每个点的入栈时间和出栈时间,这样一个闭 ...
- jquery plupload上传插件
http://www.jianshu.com/p/047349275cd4 http://www.cnblogs.com/2050/p/3913184.html demo地址: http://chap ...
- source命令用法(转载)
转自:http://zhidao.baidu.com/link?url=mNfsPHSjTEm7llgyMYx0UVNwkJmD_cxLeHtZnHcM6Ms8LDXofVHka_EzHi6GltbR ...
- 如何快速删除Linux下的svn隐藏文件及其他临时文件 (转载)
转自:http://blog.csdn.net/edsam49/article/details/5840489 在Linux下,你的代码工程如果是用svn进行管理的,要删除Linux kernel里的 ...
- js 上传头像
css .con4{width: 230px;height: auto;overflow: hidden;margin: 20px auto;color: #FFFFFF;} .con4 .btn{w ...
- spring的annotation
spring容器创建bean对象的方式: 1,使用反射调用无参构造器来创建实例(前提是这个类有无参构造器)(常规方式) 2,通过工厂类获得实例(工厂类实现了接口FactoryBean<?> ...
- 折半枚举(双向搜索)poj27854 Values whose Sum is 0
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 23757 Accep ...
- 构造 Codeforces Round #275 (Div. 2) C. Diverse Permutation
题目传送门 /* 构造:首先先选好k个不同的值,从1到k,按要求把数字放好,其余的随便放.因为是绝对差值,从n开始一下一上, 这样保证不会超出边界并且以防其余的数相邻绝对值差>k */ /*** ...
- magento “Model collection resource name is not defined” 错误
问题出现于使用Grid时,解决方案.在使用的Model处添加 public function _construct() { parent::_construct(); $this->_init( ...