Suppose i want to return 404 error view from my controller's method and i have this block of code.
try {
file_get_contents('http://www.somewebsite.com');
}
catch (\Exception $e) {
return view('errors.404'); // View::make('errors.404');
// or
return response()->view('errors.404'); // Response::view('errors.404');
// or
abort(404); // App::abort(404);
}
Each time i'll see the same view output of 404. Here is my questions.
What is the difference among view(), response()->view() and abort()?
What is the particular use cases of them?
view()
is just a shorthand for response()->view()
response()->view()
returns the specified view with the status code 200, but you are able to modify the response in many ways. For example set other headers or another status code like 301.
abort()
throws an NotFoundHttpException or HttpException and will make Laravel look for a view named like the corresponding code in views/errors
so you don't have to specify the view on your own.