What's the difference between redirect and return view()
Hi everyone,
I am a beginner in Laravel5. Can I ask a question about what is the difference between redirect and return view().In my controller, I have some codes to handle login. If authentication is passed, should I use redirect or return view().
For example,
if(!Auth::check())
return redirect('login');
else
// return redirect('home') or return view('home');
The difference is that when you return a view you are responding to a uri. For example, when someone access the /about uri you respond by returning a view.
A redirect is actually changing the uri which means you actually invoke another controller's method. For example, when someone tries to login by accessing the page /login and the authentication fails, you want to redirect him back to the login page (note that submitting the login form will result in a post request and you don't want to return view from a post request as it only meant to process data).
Another example is when a user tries to access a uri such as /profile and he's not authenticated, you can't show him the profile, you want to show him the login form, but if you return the login form view, your uri will be /profile and you will display the login form, doesn't make sense and it will complicate your code so much. What you do in this example just redirect the user to the login page which results in a page refresh and actually hitting another route.