Is it correct to use redirect route on the Laravel to make the change in the page’s Russian language?

Asked

Viewed 985 times

0

In case when logging in with Return view, it loads the home view above the /login url:

public function logon(Request $request){
        $dados = $request->all();
        $usuario = Usuario::where('email', $dados['email'])->first();

        if(Auth::check() || ($usuario && Hash::check($dados['senha'], $usuario->senha))){
            Auth::login($usuario, true);
            return view('welcome');
        }else{
            return view('login');
        }
    }

The question is whether it is a common and valid practice to use redirect, so that when logging in it changes Uri to / again:

public function logon(Request $request){
        $dados = $request->all();
        $usuario = Usuario::where('email', $dados['email'])->first();

        if(Auth::check() || ($usuario && Hash::check($dados['senha'], $usuario->senha))){
            Auth::login($usuario, true);
            return redirect()->route('home');
        }else{
            return redirect()->route('login');
        }
    }

It is correct to carry out the execution in this way?

1 answer

0


When you do the return redirect()->route('login') you are redirecting the request to the desired route, what will happen? For the route login there is a controller, in this controller the method responsible for the route will verify that the user is logged in and will return to the configured path, in the log-in:

return $this->authenticated($request, $this->guard()->user()) ?: redirect()->intended($this->redirectPath());

That is, it is nothing more than to go through the rule of authentication again and if everything is right, redirect the request to the redirectPath configured in your application and that has the default value a home.

In short: It is not wrong to do this, just be careful because when you redirect a request to another route, this other route will execute the code of the method assigned to it, sometimes you will be spending processing at all.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.