实验性分离 Validate 和 Request
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
<?php
|
||||
namespace app\admin\controller;
|
||||
|
||||
use think\Exception;
|
||||
use think\permissions\facade\Roles;
|
||||
use app\validates\RoleValidate;
|
||||
use think\permissions\facade\Permissions;
|
||||
|
@@ -3,7 +3,7 @@
|
||||
namespace app\admin\controller;
|
||||
|
||||
use app\model\UserModel;
|
||||
use app\validates\UserValidate;
|
||||
use app\admin\request\UserRequest;
|
||||
use think\permissions\facade\Roles;
|
||||
|
||||
class User extends Base
|
||||
@@ -29,13 +29,10 @@ class User extends Base
|
||||
* @time at 2018年11月12日
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function create(UserModel $userModel, UserValidate $validate)
|
||||
public function create(UserModel $userModel, UserRequest $request)
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$data = $this->request->post();
|
||||
if ($err = $validate->getErrors($data)) {
|
||||
$this->error($err);
|
||||
}
|
||||
if ($request->isPost()) {
|
||||
$data = $request->post();
|
||||
$data['password'] = generatePassword($data['password']);
|
||||
if ($userId = $userModel->store($data)) {
|
||||
// 分配角色
|
||||
@@ -55,13 +52,10 @@ class User extends Base
|
||||
* @time at 2018年11月12日
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function edit(UserModel $userModel, UserValidate $validate)
|
||||
public function edit(UserModel $userModel, UserRequest $request)
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$data = $this->request->post();
|
||||
if ($err = $validate->getErrors($data)) {
|
||||
$this->error($err);
|
||||
}
|
||||
if ($request->isPost()) {
|
||||
$data = $request->post();
|
||||
$this->giveRoles($userModel, $data['id'], $data);
|
||||
$data['password'] = generatePassword($data['password']);
|
||||
$userModel->updateBy($data['id'], $data) ? $this->success('修改成功', url('user/index')) : $this->error('修改失败');
|
||||
|
67
application/admin/request/FormRequest.php
Normal file
67
application/admin/request/FormRequest.php
Normal 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');
|
||||
}
|
||||
}
|
17
application/admin/request/UserRequest.php
Normal file
17
application/admin/request/UserRequest.php
Normal 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());
|
||||
}
|
||||
}
|
@@ -5,7 +5,7 @@
|
||||
* Date: 2018/11/12 0012
|
||||
* Time: 下午 16:31
|
||||
*/
|
||||
namespace app\validates;
|
||||
namespace app\admin\validates;;
|
||||
|
||||
use think\Validate;
|
||||
|
@@ -6,7 +6,7 @@
|
||||
* Time: 下午 18:21
|
||||
*/
|
||||
|
||||
namespace app\validates;
|
||||
namespace app\admin\validates;
|
||||
|
||||
class PermissionValidate extends AbstractValidate
|
||||
{
|
@@ -5,7 +5,7 @@
|
||||
* Date: 2018/11/14 0014
|
||||
* Time: 下午 17:42
|
||||
*/
|
||||
namespace app\validates;
|
||||
namespace app\admin\validates;
|
||||
|
||||
class RoleValidate extends AbstractValidate
|
||||
{
|
@@ -6,7 +6,7 @@
|
||||
* Time: 下午 16:38
|
||||
*/
|
||||
|
||||
namespace app\validates;
|
||||
namespace app\admin\validates;
|
||||
|
||||
class UserValidate extends AbstractValidate
|
||||
{
|
40
composer.lock
generated
40
composer.lock
generated
@@ -18,7 +18,13 @@
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/top-think/framework/zipball/f1d8ee3a91e8f504507edb5dcc49c50c47b4500f",
|
||||
"reference": "f1d8ee3a91e8f504507edb5dcc49c50c47b4500f",
|
||||
"shasum": ""
|
||||
"shasum": "",
|
||||
"mirrors": [
|
||||
{
|
||||
"url": "https://dl.laravel-china.org/%package%/%reference%.%type%",
|
||||
"preferred": true
|
||||
}
|
||||
]
|
||||
},
|
||||
"require": {
|
||||
"php": ">=5.6.0",
|
||||
@@ -69,7 +75,13 @@
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/top-think/think-captcha/zipball/54c8a51552f99ff9ea89ea9c272383a8f738ceee",
|
||||
"reference": "54c8a51552f99ff9ea89ea9c272383a8f738ceee",
|
||||
"shasum": ""
|
||||
"shasum": "",
|
||||
"mirrors": [
|
||||
{
|
||||
"url": "https://dl.laravel-china.org/%package%/%reference%.%type%",
|
||||
"preferred": true
|
||||
}
|
||||
]
|
||||
},
|
||||
"require": {
|
||||
"topthink/framework": "5.1.*"
|
||||
@@ -108,7 +120,13 @@
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/top-think/think-installer/zipball/f5400a12c60e513911aef41fe443fa6920952675",
|
||||
"reference": "f5400a12c60e513911aef41fe443fa6920952675",
|
||||
"shasum": ""
|
||||
"shasum": "",
|
||||
"mirrors": [
|
||||
{
|
||||
"url": "https://dl.laravel-china.org/%package%/%reference%.%type%",
|
||||
"preferred": true
|
||||
}
|
||||
]
|
||||
},
|
||||
"require": {
|
||||
"composer-plugin-api": "^1.0"
|
||||
@@ -149,7 +167,13 @@
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/top-think/think-migration/zipball/70c89850ca29c2eab988c7c3475d1d5331901bb8",
|
||||
"reference": "70c89850ca29c2eab988c7c3475d1d5331901bb8",
|
||||
"shasum": ""
|
||||
"shasum": "",
|
||||
"mirrors": [
|
||||
{
|
||||
"url": "https://dl.laravel-china.org/%package%/%reference%.%type%",
|
||||
"preferred": true
|
||||
}
|
||||
]
|
||||
},
|
||||
"require": {
|
||||
"topthink/framework": "5.1.*"
|
||||
@@ -188,7 +212,13 @@
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/yanwenwu/thinkphp-permission/zipball/670caf6a98a476e769fb24002aeb5feb5f2a6e69",
|
||||
"reference": "670caf6a98a476e769fb24002aeb5feb5f2a6e69",
|
||||
"shasum": ""
|
||||
"shasum": "",
|
||||
"mirrors": [
|
||||
{
|
||||
"url": "https://dl.laravel-china.org/%package%/%reference%.%type%",
|
||||
"preferred": true
|
||||
}
|
||||
]
|
||||
},
|
||||
"require": {
|
||||
"topthink/think-migration": "^2.0"
|
||||
|
@@ -13,13 +13,13 @@ return [
|
||||
// 数据库类型
|
||||
'type' => 'mysql',
|
||||
// 服务器地址
|
||||
'hostname' => '',
|
||||
'hostname' => '127.0.0.1',
|
||||
// 数据库名
|
||||
'database' => '',
|
||||
'database' => 'cms',
|
||||
// 用户名
|
||||
'username' => '',
|
||||
'username' => 'root',
|
||||
// 密码
|
||||
'password' => '',
|
||||
'password' => 'admin',
|
||||
// 端口
|
||||
'hostport' => '3306',
|
||||
// 连接dsn
|
||||
|
Reference in New Issue
Block a user