简体   繁体   中英

Change Redirect Path

So I am building this web application in laravel framework (version 5.3) and I want to change the redirect path whenever user fails login, So far I've tried this piece of code in my LoginController.php

protected $loginPath = "/my-given-path";

But it just doesn't seem to work, So I tried inspecting this AuthenticatesUsers trait and got to this file and then to RedirectUsers trait and there no such function revolving around $loginPath exists, but $redirectTo exists.


Alright so I came back to AuthenticatesUsers trait and inspected couple of functions to see this :-

public function login(Request $request)
{
    $this->validateLogin($request);

    // If the class is using the ThrottlesLogins trait, we can automatically throttle
    // the login attempts for this application. We'll key this by the username and
    // the IP address of the client making these requests into this application.
    if ($lockedOut = $this->hasTooManyLoginAttempts($request)) {
        $this->fireLockoutEvent($request);

        return $this->sendLockoutResponse($request);
    }

    $credentials = $this->credentials($request);

    if ($this->guard()->attempt($credentials, $request->has('remember'))) {
        return $this->sendLoginResponse($request);
    }

    // If the login attempt was unsuccessful we will increment the number of attempts
    // to login and redirect the user back to the login form. Of course, when this
    // user surpasses their maximum number of attempts they will get locked out.
    if (! $lockedOut) {
        $this->incrementLoginAttempts($request);
    }

    return $this->sendFailedLoginResponse($request);
}

hence I need to inspect this `sendFailedLoginResponse()' function and so here it goes :-

 protected function sendFailedLoginResponse(Request $request)
{
    return redirect()->back()
        ->withInput($request->only($this->username(), 'remember'))
        ->withErrors([
            $this->username() => Lang::get('auth.failed'),
        ]);
}

So now I tried changing redirect()->back()... to redirect('/my-given-path')... but still no luck.


I have been trying to solve this problem for the whole day and can't come up with solution and the one's asked before aren't of laravel 5.3 version. hence, I'd be glad if someone could tell what's going on and where I am being wrong?

Alright, So I found the solution, Actually I think I was doing something wrong when changing redirect()->back()... to redirect('/my-given-url')... in my AuthenticatesUsers.php file under function sendFailedLoginResponse() .


But $loginPath is no longer working and no such function exists in RedirectUsers trait, when $redirectTo exists. So I have to manually hardcode my desired path in the function of AuthenticatesUsers trait.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM