laravel 参数 1 传递给 App\Http\Controllers\Auth\LoginController::authenticated()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50083945/
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 17:41:21  来源:igfitidea点击:

Argument 1 passed to App\Http\Controllers\Auth\LoginController::authenticated()

phplaravelvalidationlaravel-5

提问by mohitkirange

Help me understand and solve such errors.

帮助我理解并解决此类错误。

ERROR :

错误 :

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_RECOVERABLE_ERROR) Type error: Argument 1 passed to App\Http\Controllers\Auth\LoginController::authenticated() must be an instance of App\Http\Controllers\Auth\Request, instance of Illuminate\Http\Request given, called in C:\proj\easywash\vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers.php on line 104

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_RECOVERABLE_ERROR) 类型错误:传递给 App\Http\Controllers\Auth\LoginController::authenticated() 的参数 1 必须是 App\Http\Controllers\Auth\Request 的实例,实例Illuminate\Http\Request 给定,在 C:\proj\easywash\vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers.php 第 104 行调用



Login Controller :

登录控制器:

    <?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Auth;
class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

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

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except'=> ['logout','userLogout']]);
    }

    public function authenticated(Request $request, $user)
        {
            if (!$user->verified) {
                auth()->logout();
                return back()->with('warning', 'You need to confirm your account. We have sent you an activation code, please check your email.');
            }
            return redirect()->intended($this->redirectPath());
        }

    public function userLogout()
    {
        Auth::guard('web')->logout();
        return redirect('/home');
    }
}

Register Controller :

注册控制器:

<?php

namespace App\Http\Controllers\Auth;
use App\User;
use App\Mail\VerifyMail;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use App\VerifyUser;
use Auth;
use DB;
use Mail;
use Illuminate\Http\Request;

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 = '/home';

    /**
     * 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)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6|confirmed',

        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data)
    {
               $user = User::create([
                    'name' => $data['name'],
                    'email' => $data['email'],
                    'password' => bcrypt($data['password']),
                ]);

                $verifyUser = VerifyUser::create([
                    'user_id' => $user->id,
                    'token' => str_random(40)
                ]);

                Mail::to($user->email)->send(new VerifyMail($user));

                return $user;


    }

    public function verifyUser($token)
    {
        $verifyUser = VerifyUser::where('token', $token)->first();
        if(isset($verifyUser) ){
            $user = $verifyUser->user;
            if(!$user->verified) {
                $verifyUser->user->verified = 1;
                $verifyUser->user->save();
                $status = "Your e-mail is verified. You can now login.";
            }else{
                $status = "Your e-mail is already verified. You can now login.";
            }
        }else{
            return redirect('/login')->with('warning', "Sorry your email cannot be identified.");
        }

        return redirect('/login')->with('status', $status);
    }
    protected function registered(Request $request, $user)
    {
        $this->guard()->logout();
        return redirect('/login')->with('status', 'We sent you an activation code. Check your email and click on the link to verify.');
    }


   }

回答by Qrazier

I also had the same problemas you and I solved that with this answer.

我也有和你一样的问题,我用这个答案解决了这个问题



ANSWER回答

Change your usestatement at LoginController:

更改您use在 LoginController的声明:

use Illuminate\Http\Request;
// And try to comment out the next line
// use App\Http\Requests;

You're overriding thismethod from the AuthenticatesUserstrait, which receives a Illuminate\Http\Request, not a App\Http\Controllers\Auth\Request

您正在从trait覆盖这个方法AuthenticatesUsers,它接收一个Illuminate\Http\Request,而不是一个App\Http\Controllers\Auth\Request