实验性分离 Validate 和 Request

This commit is contained in:
yanwenwu
2018-11-29 22:44:16 +08:00
parent 105b80e968
commit e957f9f474
10 changed files with 134 additions and 27 deletions

View File

@@ -1,7 +1,6 @@
<?php <?php
namespace app\admin\controller; namespace app\admin\controller;
use think\Exception;
use think\permissions\facade\Roles; use think\permissions\facade\Roles;
use app\validates\RoleValidate; use app\validates\RoleValidate;
use think\permissions\facade\Permissions; use think\permissions\facade\Permissions;

View File

@@ -3,7 +3,7 @@
namespace app\admin\controller; namespace app\admin\controller;
use app\model\UserModel; use app\model\UserModel;
use app\validates\UserValidate; use app\admin\request\UserRequest;
use think\permissions\facade\Roles; use think\permissions\facade\Roles;
class User extends Base class User extends Base
@@ -29,13 +29,10 @@ class User extends Base
* @time at 2018年11月12日 * @time at 2018年11月12日
* @return mixed|string * @return mixed|string
*/ */
public function create(UserModel $userModel, UserValidate $validate) public function create(UserModel $userModel, UserRequest $request)
{ {
if ($this->request->isPost()) { if ($request->isPost()) {
$data = $this->request->post(); $data = $request->post();
if ($err = $validate->getErrors($data)) {
$this->error($err);
}
$data['password'] = generatePassword($data['password']); $data['password'] = generatePassword($data['password']);
if ($userId = $userModel->store($data)) { if ($userId = $userModel->store($data)) {
// 分配角色 // 分配角色
@@ -55,13 +52,10 @@ class User extends Base
* @time at 2018年11月12日 * @time at 2018年11月12日
* @return mixed|string * @return mixed|string
*/ */
public function edit(UserModel $userModel, UserValidate $validate) public function edit(UserModel $userModel, UserRequest $request)
{ {
if ($this->request->isPost()) { if ($request->isPost()) {
$data = $this->request->post(); $data = $request->post();
if ($err = $validate->getErrors($data)) {
$this->error($err);
}
$this->giveRoles($userModel, $data['id'], $data); $this->giveRoles($userModel, $data['id'], $data);
$data['password'] = generatePassword($data['password']); $data['password'] = generatePassword($data['password']);
$userModel->updateBy($data['id'], $data) ? $this->success('修改成功', url('user/index')) : $this->error('修改失败'); $userModel->updateBy($data['id'], $data) ? $this->success('修改成功', url('user/index')) : $this->error('修改失败');

View File

@@ -0,0 +1,67 @@
<?php
/**
* UserRequest.php
* Created by wuyanwen <wuyanwen1992@gmail.com>
* Date: 2018/11/29 0029 21:56
*/
namespace app\admin\request;
use think\Request;
use think\Container;
use think\exception\HttpResponseException;
use think\Response;
use think\response\Redirect;
abstract class FormRequest extends Request
{
/**
* FormRequest constructor.
* @throws \think\Exception
*/
public function __construct()
{
parent::__construct();
$err = $this->validate();
return $this->error($err);
}
protected function error($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
{
$type = $this->getResponseType();
if (is_null($url)) {
$url = $this->isAjax() ? '' : 'javascript:history.back(-1);';
} elseif ('' !== $url) {
$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : url($url);
}
$result = [
'code' => 0,
'msg' => $msg,
'data' => $data,
'url' => $url,
'wait' => $wait,
];
if ('html' == strtolower($type)) {
$type = 'jump';
}
$response = Response::create($result, $type)->header($header)->options(['jump_template' => config('dispatch_error_tmpl')]);
throw new HttpResponseException($response);
}
/**
* 获取当前的response 输出类型
* @access protected
* @return string
*/
protected function getResponseType()
{
return !$this->isAjax()
? config('default_ajax_return')
: config('default_return_type');
}
}

View File

@@ -0,0 +1,17 @@
<?php
/**
* UserRequest.php
* Created by wuyanwen <wuyanwen1992@gmail.com>
* Date: 2018/11/29 0029 21:56
*/
namespace app\admin\request;
use app\admin\validates\UserValidate;
class UserRequest extends FormRequest
{
public function validate()
{
return (new UserValidate())->getErrors($this->post());
}
}

View File

@@ -5,7 +5,7 @@
* Date: 2018/11/12 0012 * Date: 2018/11/12 0012
* Time: 下午 16:31 * Time: 下午 16:31
*/ */
namespace app\validates; namespace app\admin\validates;;
use think\Validate; use think\Validate;

View File

@@ -6,7 +6,7 @@
* Time: 下午 18:21 * Time: 下午 18:21
*/ */
namespace app\validates; namespace app\admin\validates;
class PermissionValidate extends AbstractValidate class PermissionValidate extends AbstractValidate
{ {

View File

@@ -5,7 +5,7 @@
* Date: 2018/11/14 0014 * Date: 2018/11/14 0014
* Time: 下午 17:42 * Time: 下午 17:42
*/ */
namespace app\validates; namespace app\admin\validates;
class RoleValidate extends AbstractValidate class RoleValidate extends AbstractValidate
{ {

View File

@@ -6,7 +6,7 @@
* Time: 下午 16:38 * Time: 下午 16:38
*/ */
namespace app\validates; namespace app\admin\validates;
class UserValidate extends AbstractValidate class UserValidate extends AbstractValidate
{ {

40
composer.lock generated
View File

@@ -18,7 +18,13 @@
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/top-think/framework/zipball/f1d8ee3a91e8f504507edb5dcc49c50c47b4500f", "url": "https://api.github.com/repos/top-think/framework/zipball/f1d8ee3a91e8f504507edb5dcc49c50c47b4500f",
"reference": "f1d8ee3a91e8f504507edb5dcc49c50c47b4500f", "reference": "f1d8ee3a91e8f504507edb5dcc49c50c47b4500f",
"shasum": "" "shasum": "",
"mirrors": [
{
"url": "https://dl.laravel-china.org/%package%/%reference%.%type%",
"preferred": true
}
]
}, },
"require": { "require": {
"php": ">=5.6.0", "php": ">=5.6.0",
@@ -69,7 +75,13 @@
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/top-think/think-captcha/zipball/54c8a51552f99ff9ea89ea9c272383a8f738ceee", "url": "https://api.github.com/repos/top-think/think-captcha/zipball/54c8a51552f99ff9ea89ea9c272383a8f738ceee",
"reference": "54c8a51552f99ff9ea89ea9c272383a8f738ceee", "reference": "54c8a51552f99ff9ea89ea9c272383a8f738ceee",
"shasum": "" "shasum": "",
"mirrors": [
{
"url": "https://dl.laravel-china.org/%package%/%reference%.%type%",
"preferred": true
}
]
}, },
"require": { "require": {
"topthink/framework": "5.1.*" "topthink/framework": "5.1.*"
@@ -108,7 +120,13 @@
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/top-think/think-installer/zipball/f5400a12c60e513911aef41fe443fa6920952675", "url": "https://api.github.com/repos/top-think/think-installer/zipball/f5400a12c60e513911aef41fe443fa6920952675",
"reference": "f5400a12c60e513911aef41fe443fa6920952675", "reference": "f5400a12c60e513911aef41fe443fa6920952675",
"shasum": "" "shasum": "",
"mirrors": [
{
"url": "https://dl.laravel-china.org/%package%/%reference%.%type%",
"preferred": true
}
]
}, },
"require": { "require": {
"composer-plugin-api": "^1.0" "composer-plugin-api": "^1.0"
@@ -149,7 +167,13 @@
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/top-think/think-migration/zipball/70c89850ca29c2eab988c7c3475d1d5331901bb8", "url": "https://api.github.com/repos/top-think/think-migration/zipball/70c89850ca29c2eab988c7c3475d1d5331901bb8",
"reference": "70c89850ca29c2eab988c7c3475d1d5331901bb8", "reference": "70c89850ca29c2eab988c7c3475d1d5331901bb8",
"shasum": "" "shasum": "",
"mirrors": [
{
"url": "https://dl.laravel-china.org/%package%/%reference%.%type%",
"preferred": true
}
]
}, },
"require": { "require": {
"topthink/framework": "5.1.*" "topthink/framework": "5.1.*"
@@ -188,7 +212,13 @@
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/yanwenwu/thinkphp-permission/zipball/670caf6a98a476e769fb24002aeb5feb5f2a6e69", "url": "https://api.github.com/repos/yanwenwu/thinkphp-permission/zipball/670caf6a98a476e769fb24002aeb5feb5f2a6e69",
"reference": "670caf6a98a476e769fb24002aeb5feb5f2a6e69", "reference": "670caf6a98a476e769fb24002aeb5feb5f2a6e69",
"shasum": "" "shasum": "",
"mirrors": [
{
"url": "https://dl.laravel-china.org/%package%/%reference%.%type%",
"preferred": true
}
]
}, },
"require": { "require": {
"topthink/think-migration": "^2.0" "topthink/think-migration": "^2.0"

View File

@@ -13,13 +13,13 @@ return [
// 数据库类型 // 数据库类型
'type' => 'mysql', 'type' => 'mysql',
// 服务器地址 // 服务器地址
'hostname' => '', 'hostname' => '127.0.0.1',
// 数据库名 // 数据库名
'database' => '', 'database' => 'cms',
// 用户名 // 用户名
'username' => '', 'username' => 'root',
// 密码 // 密码
'password' => '', 'password' => 'admin',
// 端口 // 端口
'hostport' => '3306', 'hostport' => '3306',
// 连接dsn // 连接dsn