Hello Developers!
So, you want to build something cool and amazing with Laravel right now? It’s terribly awesome! One thing we all crash into is dealing with the HTTP requests. It may sound something techy or grouchy but in real, it’s just the way how your app talks to the outer world – or how users talk to your app.
Try to think of it like this way: online you ordered some food from your favorite restaurant (that’s a request!), and the food is delivered from that restaurant to you (that’s a response!). We’re doing the same thing in Laravel with data.
Why Bother with Requests?
Listen, if your app is unable to handle the request, it’s nothing but a fancy screensaver which is good for nothing . Here we need data to make things happen!
- Getting User Input: Things like forms, logins, search bars – all that stuff in this process sends data to your app.
- Talking to Other Apps (APIs): Have you ever used a weather app? It gets its data from somewhere else. That’s an API, and we use requests to talk to it.
- Keeping Things Safe: We need to make sure of getting good data while working with it and not letting anything bad.
Let’s Get Our Hands Dirty (But Not Too Dirty!)
So, Laravel uses this thing called Illuminate\Http\Request.So, don’t be scared of the long name it has. It’s just a tool which is used to help us with work with requests.
Grabbing the Good Stuff (Input Data)
Just try to imagine you’ve got a form where someone is entering his name and email. Here’s how we snag that info:
use Illuminate\Http\Request; Route::post('/submit', function (Request $request) { // Getting everything they typed in $allTheirStuff = $request->all(); // Just their name $theirName = $request->input('name'); // Or just $request->name; // Their email, or "guest@example.com" if they forgot $theirEmail = $request->input('email', 'guest@example.com'); // If they picked a bunch of tags $theirTags = $request->input('tags.*'); // Was that "publish" checkbox checked? $isItPublished = $request->boolean('is_published'); // Turning a string to a number. $theirId = $request->integer('id'); // Making sure a value is a string. $theirTitle = $request->string('title'); // Turning a string into a date. $theirDate = $request->date('created_at'); // Did they upload a photo? if ($request->hasFile('photo')) { $theirPhoto = $request->file('photo'); $photoPath = $theirPhoto->store('uploads'); } // ... Now do something with that data! });
Have you seen it? It’s not too scary as you think of it. We’re just grabbing the data and doing stuff with it.
What Kind of Request Is It?
Sometimes, you really need to know how the data has got to you. Was it a form submission (POST)? Were they just asking for a page (GET)? Then
use Illuminate\Http\Request; Route::get('/profile', function (Request $request) { if ($request->isMethod('GET')) { // They're just looking at their profile } if ($request->is('profile/*')) { //Anything that starts with profile/ } if ($request->expectsJson()) { // They want data as JSON (for apps, usually) } $authToken = $request->header('Authorization'); $device = $request->userAgent(); // ... More stuff! });
Laravel Request Validation Checking
It’s a very foolish offence to just trust everything people send to us. Laravel has an easy to built-in way technique to check if the data is validated or not.
use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; Route::post('/submit', function (Request $request) { $checkTheData = Validator::make($request->all(), [ 'name' => 'required|string|max:255', 'email' => 'required|email|unique:users', 'password' => 'required|min:8', ]); if ($checkTheData->fails()) { return response()->json(['errors' => $checkTheData->errors()], 422); } // Data's good! Let's do something with it. });
Read More
Mastering Laravel Controllers: Your Gateway to Dynamic Web Applications
Laravel Middleware The Secret Sauce of Your Web Apps | TutorialDev
Laravel Routing: Basic Routes, Parameters & Groups | TutorialDev
Request Objects
For the complex validation, Laravel’s request objects are very useful.
use App\Http\Requests\StorePostRequest; Route::post('/posts', function (StorePostRequest $request) { // The incoming request is valid... // Retrieve the validated input data... $validated = $request->validated(); // ... });
Wrapping Up
While working in this field, handling HTTP requests is something we do all the time as developers. But,Laravel makes it pretty fancy and straightforward. So you don’t need to be afraid of experimenting and play around with it!
Happy coding!
Let me know in the comment box if this tone feels more like a casual chat or not!