Laravel 8 Tutorial - HTTP Requests and HTML Form

In this video, we will learn about HTTP Requests and HTML Forms in Laravel 8. Occasionally, developers may need to fetch an instance of the ongoing HTTP request. In Laravel 8, we can obtain this by using the Request class in our controller.

This will automatically fetch the incoming HTTP request instance. So, let's see some usages of HTTP Request. For that, just open the UserController. You can notice that Illuminate\Http\Request is already attached to all the controllers that we create with the artisan command.

Now, we can use this Request Class inside our controller method. So, I am just going to type here Request $request. Now, we can use the $request object here for accessing its methods. So, just inside the index method, add the following code:


public function index(Request $request)
{
return $request->method();
}

Now, save the file and check it on the browser. So, switch to the browser, add /users and press enter. Now, you can see here the requested method, which is get, is showing here.

Now, see the Request path method. The path method returns the request's path information. So, add here:


public function index(Request $request)
{
return $request->path();
}

So, if the incoming request is targeted at http://localhost:8000/users, the path method will return users. So, let's check, switch to the browser and refresh the page. Now, you can see here it shows users, which is the URL path.

Now, another Request method is URL, which is used to retrieve the URL from the incoming request. So, let's see the URL method:


public function index(Request $request)
{
return $request->url();
}

Now, save and switch to the browser and refresh the page. Now, you can see the URL, which is localhost:8000/users. If I add some query string in the URL, let's say ?name=jenifer&age=25, now enter. You can see here the query string is not showing here.

So, for showing the complete URL with the query string, then use the fullUrl() method. So, just go to the controller and add here fullUrl()


public function index(Request $request)
{
return $request->fullUrl();
}

Now, save it and switch to the browser and refresh the page. Ok, now you can see here it shows the full URL with the query string.

Alright, now we are going to see how we can use Form in Laravel 8 for sending a request to the server. Let's create a controller first. Switch to the command prompt and run the following command:


php artisan make:controller LoginController

Open the LoginController and create a method:


public function index()
{
return view('login');
}

Now, create this view, so go inside the resources/views directory and here let's create a login.blade.php view file. And also create a route for this view, so go to the web.php file and create the route:


Route::get('/login',[LoginController::class,'index');

Now, go to the login.blade.php file and here first of all add the HTML5 boilerplate. So, type Html:5 and press tab. Now, add the title here login, and inside the body, just add the form.

Before adding the form, for styling the page, I am just going to add Bootstrap 4 CDN. So, for that, go to getbootstrap.com, then click on getting started, and from here, just copy this CSS and put it inside the head tag. Now, copy the JS and put it just before the closing body tag. Ok, now Bootstrap 4 is added to our project.

Now, we can use the Bootstrap 4 class. So, just add a section, then a container and row, and create a column:


<div class="container">
<div class="row">
<div class="col-md-6 offset-md-3">
<div class="card">
<div class="card-heading">
Login
</div>
<div class="card-body">
<form method="POST" action="{{route('login.submit')}}">
@csrf
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" name="email" />
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" name="password" />
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
</div>
</div>

Now, go to the LoginController. Here, create a method for login submit. Now, create a route for the form submission:


public function loginsubmit()
{
return "Form Submitted";
}

Now, create a route for this function. So, go to the web.php file and write here:


Route::post('/login',[LoginController::class,'loginsubmit')->name('loginsubmit');

Go to the browser and just type here /login, and fill some data here, and now click on submit. Now, you can see the form is now submitted.

You may also retrieve all of the input data as an array using the all method. For that, just add the following code in the login submit method: