laravel 在 Lumen 中找不到“验证器”类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32848048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:25:18  来源:igfitidea点击:

Class 'Validator' not found in Lumen

phpvalidationlaravellumen

提问by epod

try to create validator manually in Lumen. The official documentation is written:

尝试在 Lumen 中手动创建验证器。官方文档是这样写的:

<?php

namespace App\Http\Controllers;
use Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PostController extends Controller
{
     /**
     * Store a new blog post.
     *
     * @param  Request  $request
     * @return Response
     */
     public function store(Request $request)
     {
        $validator = Validator::make($request->all(), [
            'title' => 'required|unique:posts|max:255',
            'body' => 'required',
        ]);

        if ($validator->fails()) {
            return redirect('post/create')
                        ->withErrors($validator)
                        ->withInput();
        }

        // Store the blog post...
     }
}

I wrote

我写

<?php

namespace App\Http\Controllers;
use Laravel\Lumen\Routing\Controller as BaseController,
    Validator;

class Welcome extends BaseController
{
    public function index()
    {
        $validator = Validator::make(
            ['test' =>'TestValidation'],
            ['test' => 'required|unique:posts|max:255']
        );
    }
}

but Lumen returns fatal error: Fatal error: Class 'Validator' not found in ...

但 Lumen 返回致命错误: 致命错误:在 ...

I have tried to do like in Laravel 5:

我曾尝试在 Laravel 5 中这样做:

use Illuminate\Support\Facades\Validator;

but then Lumen returns Fatal error: Call to a member function make() on a non-object in

但随后 Lumen 返回 致命错误:在非对象上调用成员函数 make()

Somebody knows how to use the Validator class in Lumen? Thank you.

有人知道如何使用 Lumen 中的 Validator 类吗?谢谢你。

回答by baao

Validator is a facade. Facades aren't enabled by default in lumen.

验证器是一个门面。默认情况下,流明中不启用外观。

If you would like to use the a facade, you should uncomment the

如果你想使用a门面,你应该取消注释

$app->withFacades();

call in your bootstrap/app.phpfile.

调用您的bootstrap/app.php文件。

回答by Arnold Balliu

This is for Lumen version 5.3 (as shown in the docs):

这是针对 Lumen 5.3 版(如文档中所示):

use Illuminate\Http\Request;

$app->post('/user', function (Request $request) {
    $this->validate($request, [
    'name' => 'required',
    'email' => 'required|email|unique:users'
 ]);

    // Store User...
});

https://lumen.laravel.com/docs/5.3/validation

https://lumen.laravel.com/docs/5.3/validation