The normal usage of Laravel on a website is that a client request to an endpoint results in a response, one way or the other. The request could be anything from a typical browser request for a given website for HTML or files, to an automated request from a client server to an API endpoint you publish for structured data responses like JSON or XML. Either way, at some point you need to create and return a response in your controller- and Laravel comes ready to do so with lots of options available.
Responding from a route without a controller
You can respond directly from a route, without making a controller and action method. This is for illustration only, because most projects probably benefit greatly from a strict MVC pattern adherence.
Route::get('/', function() { return 'Hello World'; });
Responding with a HTML view
Alternatively to responding with a simple text string, you can respond with a view. The array of key/values are placeholder values to be inserted into the view.
Route::get('/', function() { return view('greeting', ['name' => 'James']); });
Responding with JSON data
Below is an example of returning structured data with Laravel. In this case it is JSON, but it could be whatever format you need. The array of data will be converted into JSON by the response handler.
Route::get('/', function() { return response()->json(['name' => 'Abigail', 'state' => 'CA']); });
Method chaining for response objects
When using the response methods, you essentially create and handle response objects that are chainable. Therefore you can easily add further instructions to your response. Below is an example of a view that also sets a cookie and header value.
return response()->view('hello') ->header('Content-Type', $type) ->withCookie(cookie('name', 'value'));
Redirects
Redirects are a special response type. They don’t return any specific content to the user, but include an instruction to go somewhere else instead. Below are a few examples of redirects.
// Redirect to a specific path return redirect('user/login'); // Redirect to a specific path including a message to be shown return redirect('user/login')->with('message', 'Login Failed'); // Redirect back to previous page return redirect()->back(); // Redirect back to previous page including input data // (useful for repopulating a form with invalid data, so the user input is preserved) return redirect()->back()->withInput();
Other responses
It is important to structure your responses and make sure you deliver the expected output to the client. Using Laravel’s built-in methods for creating responses ensures that you create and send them consistently, and you can easier implement changes.
You can create your own response types if you have special needs, thus adapting or extending the included helpers for your scenario. Avoid building own response handlers and using PHP’s native functions without Laravel’s. Using Laravel’s wrappers is the right way of ensuring central control over your responses via the framework.
No comments yet (leave a comment)