Laravel - Redirect上不存在Method [validate]

时间:2018-04-16 16:51:43

标签: php laravel laravel-5.3

我知道在stackoverflow上已经多次询问过这个问题,但我找到的每个人都没有帮助我。每当我尝试注册用户时,其中包含的内容无效(电子邮件已经注册,密码时间不够等),我会在标题中收到消息。

我的RegisterController.php:

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        $validator =  Validator::make($data, [
            'name' => 'required|max:255',
            'lastname' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6|confirmed',
        ]);

        if($validator->fails())
        {
            return Redirect::back()->withInput()->withErrors($validator);
        }

        return $validator;
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        $confirmation_code = str_random(30);


        Mail::send('user.verify', ['code' => $confirmation_code], function($message) {
            $message->to(Input::get('email'), Input::get('username'))
                ->subject('Verify your email address');
        });


//        Flash::message('Thanks for signing up! Please check your email.');

        $user =  new User;
        $user->lastname = $data['lastname'];
        $user->name = $data['name'];
        $user->email = $data['email'];
        $user->password = bcrypt($data['password']);
        $user->confirmation_code = $confirmation_code;
        $user->save();


        return $user;
    }


}

我需要帮助,希望有人可以帮助解决这个问题。如果需要任何其他课程,我很乐意提供。

编辑:更新,因为我注意到原因。

1 个答案:

答案 0 :(得分:0)

验证器方法应返回Validator。您看到的错误是因为您返回了Redirect响应,并且某些代码假定您返回Validator并在其上调用$ validator-&gt; validate()。

您需要抛出HttpResponseException。您的异常处理程序应捕获这些并相应地呈现响应;在这种情况下执行重定向。