覆盖postLogin功能

时间:2017-01-09 16:31:37

标签: laravel laravel-5 laravel-5.2

我试图在我的laravel应用程序上覆盖postLogin函数,但是给了我一个"Class App\Http\Controllers\Auth\Request does not exist"。基本上在我把函数postLogin之前它从来没有给我这个错误,登录工作正常,但现在我试图覆盖postLogin函数,因为我需要插入一些更多的逻辑,给我错误。 我的laravel版本是5.2

这是我的代码:

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

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


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



    /**
     * Overwrite postLogin function.
     *
     * @return void
     */

    public function postLogin(Request $request)
    {
        dd($request->all());
    }

    /**
     * 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|max:255',
            'email' => 'required|email|max:255|unique:users',
            'passwords' => 'required|min:6|confirmed',
        ]);
    }

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






}

2 个答案:

答案 0 :(得分:1)

这是因为postLogin()正在尝试注入Request类,但您忘记定义它的命名空间。

在课程顶部的namespace子句后面添加此行:

use Illuminate\Http\Request;

这将解决问题。

答案 1 :(得分:0)

注意use AuthenticatesAndRegistersUsers

你应该在你的代码中构建一个特征,并将所有代码从AuthenticatesAndRegistersUsers粘贴到新的特征中;然后将你的逻辑添加到新特征中。