Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 5.6美元此->;验证vs验证程序::make()_Php_Laravel_Validation - Fatal编程技术网

Php Laravel 5.6美元此->;验证vs验证程序::make()

Php Laravel 5.6美元此->;验证vs验证程序::make(),php,laravel,validation,Php,Laravel,Validation,我看到还有一些其他问题,他们问$this->validate和Validator::make()之间有什么区别。但他们并没有真正回答我想知道的概念性问题 每一个都有正确的用途吗?比如什么时候用一个和另一个 我目前使用它的方式是在我的API类中,我使用if-else和$validator::make()(如下所示),而在我的程序的Web部分中,我使用$this->validate()(如下所示) 这是正确的使用方法吗 $validator::make: public function store(

我看到还有一些其他问题,他们问
$this->validate
Validator::make()
之间有什么区别。但他们并没有真正回答我想知道的概念性问题

每一个都有正确的用途吗?比如什么时候用一个和另一个

我目前使用它的方式是在我的API类中,我使用if-else和
$validator::make()
(如下所示),而在我的程序的Web部分中,我使用$
this->validate()
(如下所示)

这是正确的使用方法吗

$validator::make:

public function store(Request $request)
    {
        $validator = Validator::make($request->all(),[
            'name' => 'required',
            'url' => 'required',
            'isPublic' => 'required'
        ]);

        if($validator->fails()){
            return response($validator->messages(), 200);
        } else {

            Helpers::storeServer($request);

            return response()->json([
                'message'=> ['Server Stored']
            ]);
        }
    }
 public function store(Request $request)
    {

        $this->validate($request, [
            'name' => 'required',
            'url' => 'required',
            'isPublic' => 'required'
        ]);

        Helpers::storeServer($request);

        return redirect('dashboard')->with('success', 'Server stored');
    }
$validator = Validator::make() //creates an instance of the validator for further operations

// You can even do after the creating the instance of validator:
$this->validateWith($validator, $request)

//Lets do both steps above i.e the validation and trigger the Validation Exception if there's failure.
$this->validate($request, [...rules...], [...messages..]) 
$this->验证:

public function store(Request $request)
    {
        $validator = Validator::make($request->all(),[
            'name' => 'required',
            'url' => 'required',
            'isPublic' => 'required'
        ]);

        if($validator->fails()){
            return response($validator->messages(), 200);
        } else {

            Helpers::storeServer($request);

            return response()->json([
                'message'=> ['Server Stored']
            ]);
        }
    }
 public function store(Request $request)
    {

        $this->validate($request, [
            'name' => 'required',
            'url' => 'required',
            'isPublic' => 'required'
        ]);

        Helpers::storeServer($request);

        return redirect('dashboard')->with('success', 'Server stored');
    }
$validator = Validator::make() //creates an instance of the validator for further operations

// You can even do after the creating the instance of validator:
$this->validateWith($validator, $request)

//Lets do both steps above i.e the validation and trigger the Validation Exception if there's failure.
$this->validate($request, [...rules...], [...messages..]) 

不,他们用两种不同的方式做同样的事情。我的意思是,
$this->validate()
在validation类上调用
make()
方法。如果您查看ValidateRequests.php,它由控制器扩展的controller.php实现。
validate()
方法调用:

$validator = $this->getValidationFactory()
             ->make($request->all(), $rules, $messages, $customAttributes);
因此它最终使用
make()
方法。它的处理方式有所不同,因为
$this->validate
调用:

if ($validator->fails()) {
    this->throwValidationException($request, $validator);
}

因此,使用
Validator::make()
将允许您自己处理异常,而不是
$this->validate()
自动为您抛出验证异常。这对于在重定向之前执行某些操作非常有用。您在第一个示例中展示了这一点,因为您在决定如何处理验证之前会检查验证是否失败。在第二个示例中,您知道如果验证失败,它将自动拒绝请求

如果
$this->validate
上的任何规则失败,将自动抛出错误

$validator::make:
您可以轻松处理错误,可能需要将任何数据包装到array()并传递到
$validator::make:
进行验证

如果你想使用FormRequest+路由模型绑定

你的商店()看起来像这样

public function store(YourFormRequestRules $request)
    {
        Helpers::storeServer($request);

        return redirect('dashboard')->with('success', 'Server stored');
    }

public function update(YourFormRequestRules $request, Post $post)
{
    $post->title = $request->input('title');
    $post->save();
    return redirect()->route('example.index');
}

使用
$this->validate()
YourController
扩展了一个
Controller
类,该类使用
validateRequests
特性,它如下所示:

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}
validateRequests
trait本身由基本控制器获得的几种方法和您自己的控制器组成,其中包括:

validateWith($validator, Request $request = null)
...
validate(Request $request, array $rules, array $messages = [], array $customAttributes = [])
...
validateWithBag($errorBag, Request $request, array $rules, array $messages = [], array $customAttributes = [])
... and so on...
这些方法有助于使一些验证和请求错误处理更加方便,事实上,考虑到前面提到的
validate()
方法

当传递的请求中存在验证错误时,它可以帮助您处理响应,而无需使用不必要的逻辑来编写控制器;i、 e当它是一个ajax调用时,它返回一个带有json主体的422响应,同时返回填充错误包并填充
$errors
变量,以便在非ajax的刀片模板中使用

总之,这只会在您不想通过创建Validator实例来进行手动验证的情况下对您有所帮助,以便让开发人员专注于实际工作;)

更新:

public function store(Request $request)
    {
        $validator = Validator::make($request->all(),[
            'name' => 'required',
            'url' => 'required',
            'isPublic' => 'required'
        ]);

        if($validator->fails()){
            return response($validator->messages(), 200);
        } else {

            Helpers::storeServer($request);

            return response()->json([
                'message'=> ['Server Stored']
            ]);
        }
    }
 public function store(Request $request)
    {

        $this->validate($request, [
            'name' => 'required',
            'url' => 'required',
            'isPublic' => 'required'
        ]);

        Helpers::storeServer($request);

        return redirect('dashboard')->with('success', 'Server stored');
    }
$validator = Validator::make() //creates an instance of the validator for further operations

// You can even do after the creating the instance of validator:
$this->validateWith($validator, $request)

//Lets do both steps above i.e the validation and trigger the Validation Exception if there's failure.
$this->validate($request, [...rules...], [...messages..]) 
检查:


验证器助手让我更开心

$validator = validator()->make(request()->all(), [
    'type' => 'required|integer'
]);

if ($validator->fails())
{
    redirect()->back()->with('error', ['your message here']);
}

Laravel提供帮助,使开发更具说服力。

而且你有我最喜欢的帮助。@RonvanderHeijden哇,我没有在这一页上绊倒。我一定把它读一遍!谢谢你的回复,这就是我所想的我只是想知道社区是怎么想的。几个月前,在我的实习期间,我刚刚进入了laravel框架,甚至php,我喜欢它,所以我正试图深入框架的根源,尽可能多地学习!听起来不错。我会尝试浏览一些源文件并从中学习。通过查看Controller.php、Model.php等一些基本文件,您可以获得很多信息。有时当我在工作中感到无聊时,我最终会通过兔子洞学到一些有用的东西。因此,在将请求传递给控制器之前,会“扫描”错误?FormRequest允许您重用规则,大多数用于存储/更新,如果找不到id,路由模型绑定将抛出404,正如您所看到的,我在更新方法中没有使用find(id),我一定会查看Jubeel文档,了解如何在应用程序中使用它!非常感谢您的回复!