seffeng / laravel-basics
Laravel extension basics
v3.4.10
2024-09-14 08:54 UTC
Requires
- php: >=7.2.5
- laravel/framework: >=7.0
- seffeng/laravel-helpers: >=0.1
- seffeng/laravel-rules: >=0.1
- dev-master
- v3.4.10
- v3.4.9
- v3.4.8
- v3.4.7
- v3.4.6
- v3.4.5
- v3.4.4
- v3.4.3
- v3.4.2
- v3.4.1
- v3.4.0
- v3.3.2
- v3.3.1
- v3.3.0
- v3.2.3
- v3.2.2
- v3.2.1
- v3.2.0
- v3.1.9
- v3.1.8
- v3.1.7
- v3.1.6
- v3.1.5
- v3.1.4
- v3.1.3
- v3.1.2
- v3.1.1
- v3.1.0
- v3.0.1
- v3.0.0
- 2.x-dev
- v2.2.3
- v2.2.2
- v2.2.1
- v2.2.0
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.16
- v2.0.15
- v2.0.14
- v2.0.13
- v2.0.12
- v2.0.11
- v2.0.10
- v2.0.9
- v2.0.8
- v2.0.7
- v2.0.6
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0
- v1.0.1
- v1.0
This package is auto-updated.
Last update: 2024-12-14 09:41:30 UTC
README
安装
# laravel7、laravel8 $ composer require seffeng/laravel-basics # laravel6 $ composer require seffeng/laravel-basics=^2.*
目录说明
├─Base 基础类
│ Controller.php 控制器
│ FormRequest.php 表单验证
│ Model.php 数据库
│ Service.php 服务
├─Constants 常量定义
│ DeleteConst.php 删除
│ ErrorConst.php 错误
│ StatusConst.php 状态
│ TypeConst.php 类型
├─Exceptions 异常
│ BaseException.php 异常
│ Handler.php 异常处理器
├─Helpers 帮助类
│ Arr.php 数组处理
│ Json.php Json处理
│ ReplaceArrayValue.php
│ TimeHelper.php 时间处理
│ UnsetArrayValue.php
└─Rules 验证规则
Password.php 密码
Phone.php 手机号
示例
/** * TestRequest.php * 表单验证示例 */ namespace App\Http\Requests; use Seffeng\Basics\Base\FormRequest; use Seffeng\Basics\Rules\Phone; class TestRequest extends FormRequest { protected $fillable = ['phone', 'password']; public function rules() { return [ 'phone' => [ 'required', new Phone() ], 'password' => 'required' ]; } public function messages() { return array_merge(parent::messages(), [ ]); } public function attributes() { return array_merge(parent::attributes(), [ 'phone' => '手机号', 'password' => '密码', ]); } } /** * TestController.php * 表单验证示例 - 控制器 */ namespace App\Http\Controllers; use Illuminate\Support\Facades\Validator; use Seffeng\Basics\Base\Controller; use Illuminate\Http\Request; use App\Http\Requests\TestRequest; class TestController extends Controller { public function index(Request $request) { $form = new TestRequest(); $data = $form->load($request->all()); $validator = Validator::make($data, $form->rules(), $form->messages(), $form->attributes()); $errors = $form->getErrorItems($validator); if ($form->getIsPass()) { return $this->responseSuccess($form->getFillItems()); } return $this->responseError($errors['message'], $errors['data']); } }
// 验证成功 { "status": "success", "data": { "phone": "13800138000", "password": "123456" }, "message": "success" } // 验证失败 { "status": "error", "code": 1, "data": { "phone": [ "手机号格式错误!" ], "password": [ "密码不能为空!" ] }, "message": "手机号格式错误! 密码不能为空!" }