PHP code example of songyz / laravel_validator
1. Go to this page and download the library: Download songyz/laravel_validator library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
songyz / laravel_validator example snippets
Songyz\Providers\ValidationServiceProvider::class,
Songyz\Providers\ValidatorConfigProvider::class,
$app->register(Songyz\Providers\ValidationServiceProvider::class);
$app->register(Songyz\Providers\ValidatorConfigProvider::class);
$app->register(Songyz\Providers\LumenFoundationServiceProvider::class);
public function add()
{
/**
* 1、进行必要参数验证
* 需要验证 username 不能为空
* moible 不能为空 且必须是手机号
* 2、接收参数
* 3、调用方法入库
* ...
*/
echo time();
}
public function update()
{
/**
* 1、进行必要参数验证
* 需要验证 username 不能为空
* moible 不能为空 且必须是手机号
* id 不能为空 且必须是数字
* 2、验证主键Id是否存在
* 2、接收参数
* 3、调用方法入库
* ...
*/
}
public function del()
{
/**
* 1、进行必要参数验证
* 需要验证 id 不能为空 且必须是数字
* 2、验证主键Id是否存在
* 2、接收参数
* 3、调用方法入库
* ...
*/
}
use App\Http\Requests\UserRequest;
public function add(UserRequest $request)
{
echo 'add';
/**
* 1、进行必要参数验证
* 需要验证 username 不能为空
* moible 不能为空 且必须是手机号
* 2、接收参数
* 3、调用方法入库
* ...
*/
echo time();
}
public function update(UserRequest $request)
{
echo 'update';
/**
* 1、进行必要参数验证
* 需要验证 username 不能为空
* moible 不能为空 且必须是手机号
* id 不能为空 且必须是数字
* 2、验证主键Id是否存在
* 2、接收参数
* 3、调用方法入库
* ...
*/
}
public function del(UserRequest $request)
{
echo 'del';
/**
* 1、进行必要参数验证
* 需要验证 id 不能为空 且必须是数字
* 2、验证主键Id是否存在
* 2、接收参数
* 3、调用方法入库
* ...
*/
}
use Songyz\Exceptions\ValidatorFailureException;
public function render($request, Exception $exception)
{
//如果是ajax请求 则返回接口形式的数据
if ($request->ajax()) {
$message = $exception->getMessage();
$defaultMessage = '网络开小差喽 请稍后...';
if ($exception instanceof ValidatorFailureException) {
//处理你的逻辑...
}
$code = strval($exception->getCode() == '0' ? '1' : $exception->getCode());
$jsonData = $data ?? [];
return response()->json(['code' => $code, 'message' => $message, 'data' => $jsonData], 200, [
'Content-type' => 'application/json'
], JSON_UNESCAPED_UNICODE);
}
return parent::render($request, $exception);
}
Route::prefix('users')->group(function () {
Route::any('add', 'UsersController@add');
Route::any('update', 'UsersController@update');
Route::any('del', 'UsersController@del');
});
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Psr\Log\LoggerInterface;
use Songyz\Exceptions\ValidatorFailureException;
use Throwable;
class Handler extends ExceptionHandler
{
/**
* 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',
];
/**
* 为什么会写这个方法
* 以前是 error 级别,这是错误级别
* 为了将级别设置为 info 所以写的这个方法
* 将ApiException 和 ValidatorFailureException 这两个异常记录为 info 级别
*
* Report or log an exception.
*
* @param \Throwable $exception
* @return void
*
* @throws \Throwable
*/
public function report(Throwable $exception)
{
if ($this->shouldntReport($exception)) {
return;
}
if (is_callable($reportCallable = [$exception, 'report'])) {
return $this->container->call($reportCallable);
}
try {
$logger = $this->container->make(LoggerInterface::class);
} catch (\Exception $ex) {
throw $ex;
}
$context = array_merge(
$this->exceptionContext($exception),
$this->context(),
['exception' => $exception]
);
$loggerLevel = 'error';
if ($exception instanceof ApiException || $exception instanceof ValidatorFailureException) {
$context = [
'exception' => 'ApiException',
'file' => $exception->getFile(),
'line' => $exception->getLine()
];
$loggerLevel = 'info';
}
$logger->{$loggerLevel}($exception->getMessage(), $context);
}
/**
* Render an exception into an HTTP response.
*
* 如果是ajax请求,则返回 json格式
*
* 否则返回正常的web页面
*
* @param \Illuminate\Http\Request $request
* @param \Throwable $exception
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Throwable
*/
public function render($request, Throwable $exception)
{
//如果是ajax请求 则返回接口形式的数据
if ($request->ajax()) {
$message = '网络开小差喽 请稍后...';
if ($exception instanceof ApiException || $exception instanceof ValidatorFailureException) {
$message = $exception->getMessage();
}
//开发环境将详细错误日志打印出来,方便排查问题
if (app()->environment() == "development") {
$data = [
'exception' => get_class($exception),
'file' => $exception->getFile(),
'line' => $exception->getLine()
];
}
$code = strval($exception->getCode() == '0' ? '1' : $exception->getCode());
return response()->json(get_return_json($data ?? [], $code, $message), 200, [], JSON_UNESCAPED_UNICODE);
}
return parent::render($request, $exception);
}
}
namespace App\Exceptions;
use Throwable;
/**
*
* Class ApiException
* @package App\Exceptions
*/
class ApiException extends \Exception
{
public $data;
public function __construct(string $message = "", $code = 1, array $data = [], Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
$this->data = $data;
}
}
shell script
php artisan vendor:publish --provider="Songyz\Providers\ValidatorConfigProvider"
shell script
php artisan mack:controller UsersController
shell script
php artisan songyz:make:request --request_name=UserRequest