Updated on Jul 11, 2024
It is time to discuss requests and responses in the Laravel framework, two fundamental concepts that drive any Laravel application. We mentioned them briefly at the beginning of this tutorial when we listed some important terminology and will not discuss them in more detail. Let's begin with requests.
You Will Read About:
You have probably heard this word a lot when it comes to applications or programming. What exactly is a request, though? Typically, in the context of programming – and Laravel itself, a request is the interaction an application receives from a user. It is called a request because this interaction requests a response from the application based on the input from the user. A basic example of this is when you open a website. Websites are web applications, so when you open one, it receives a request to present the homepage to your browser. It then sends a response to that request, and the cycle repeats each time a user takes an action. Another way to think of this is to imagine that each time you click on a button on a website, this request-response cycle is triggered. The mouse click is the request.
In programming, the word "request" most commonly refers to HTTP requests since those are the ones most applications use and rely on to function. The same applies to Laravel. As we mentioned, requests are fundamental to Laravel's operation, and it handles them rather elegantly. It is a six-step process on a most basic level, so let's break it down and look at how exactly Laravel handles HTTP requests.
Middleware can increase the number of steps a request takes until it returns a response. We have mentioned it in our index of important terminology. Simply put, though, middleware is code that can be inserted throughout the request-response sequence and can alter how it acts. For instance, in our example, middleware would handle user authentication if the provided user had the correct permissions to log in.
That is the chain of events that requests go through. As you can see, it ends with the return of a response, which we will discuss next.
As you read above, the response is the final step of the request-response cycle. That makes sense! Now, let's explain what a response is.
To begin with, the easiest way to understand a response is to think of what you see on your screen when you interact with a given application or website. When you click a button, the change that occurs is the application's response: whether you are taken to a new page or the current page updates in real-time are both examples of a response.
When talking about responses, we typically mean HTTP responses, as they are the most commonly used ones, including in Laravel. To return a response, use the return model and then place the desired function in parenthesis after it. For example, return view('welcome');. That line of code returns the welcome view when the request is made to that response. There are several common HTTP response types, and they are below.
// Action: user authenticated
Route::post('/signin', function () {
// Use the response() helper function to create a redirect response:
return response()->redirect('/dashboard');
});
Route::post('/download', function () {
return response()->download('file_path/file_name.zip');
});
To round out our look at responses in Laravel, we will discuss response headers and status codes. Firstly, the response headers. In Laravel, you can assign custom headers to your responses by using the header() method. You can use the header() method and a line of code like this to set up a single header.
return response('Hello')->header('Content-Type', 'text/plain');
As you can see, this header sets the content type of our Hello response to text/plain. You can use code like this to set up multiple headers.
return response('')->withHeaders([
'Content-Type' => 'text/plain',
'X-Custom-Header' => 'Custom Value',
]);
Use the withHeaders() methods and specify all the headers you wish within square brackets, separated by a new line. A list of all available HTTP headers and an explanation of their functions can be found here.
And finally, let's take a look at status codes within Laravel. A status code is a three-digit number accompanying a response to indicate the result of a request. You might be familiar with status code 404 if you have ever tried to access a page that does not exist. That is precisely what the code means: the resource you are looking for is not found on the server. You can use the response() function to set up a status code. Here is an example.
return response('Not Found')->setStatusCode(404);
Since there are many status codes, we recommend you take a look at this article, where they are all outlined and ready for use.
That concludes our look at responses in Laravel. As you can see, they are an important part of the framework's mechanism. Without them, we would be clicking away on our computers, and nothing would change.