Class' Validator'没有在Lumen找到

时间:2015-09-29 15:30:40

标签: php validation laravel lumen

尝试在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...
     }
}

我写了

<?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']
        );
    }
}

但是Lumen返回致命错误: 致命错误:Class&#39; Validator&#39;没有找到......

我试着在Laravel 5中做过:

use Illuminate\Support\Facades\Validator;

然后Lumen回来了 致命错误:在

中的非对象上调用成员函数make()

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

2 个答案:

答案 0 :(得分:18)

验证者是一个门面。在流明中默认情况下不启用外墙。

如果您想使用外观,则应取消注释

<p class="keep">ThisiswaytoolongThisiswaytoolongThisiswaytoolongThisiswaytoolongThisiswaytoolong</p>
<p class="break">ThisiswaytoolongThisiswaytoolongThisiswaytoolongThisiswaytoolongThisiswaytoolong</p>

拨打$app->withFacades(); 文件。

答案 1 :(得分:1)

这是针对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