Skip to main content

Laravel – Request

Laravel – Request

In this chapter, you will learn in detail about Requests in Laravel.

Retrieving the Request URI

The path method is used to retrieve the requested URI. The is method is used to retrieve the requested URI which matches the particular pattern specified in the argument of the method. To get the full URL, we can use the url method.

Example

Step 1 − Execute the below command to create a new controller called UriController.
Step 2 − After successful execution of the URL, you will receive the following output −
uricontroller
uricontroller
Step 3 − After creating a controller, add the following code in that file.
app/Http/Controllers/UriController.php
Step 4 − Add the following line in the app/Http/route.php file.
app/Http/route.php
Step 5 − Visit the following URL.
Step 6 − The output will appear as shown in the following image.
Path Method

Retrieving Input

The input values can be easily retrieved in Laravel. No matter what method was used get or post, the Laravel method will retrieve input values for both the methods the same way. There are two ways we can retrieve the input values.
  • Using the input() method
  • Using the properties of Request instance

Using the input() method

The input() method takes one argument, the name of the field in form. For example, if the form contains username field then we can access it by the following way.

Using the properties of Request instance

Like the input() method, we can get the username property directly from the request instance.

Example

Observe the following example to understand more about Requests −
Step 1 − Create a Registration form, where user can register himself and store the form at resources/views/register.php
Step 2 − Execute the below command to create a UserRegistrationcontroller.
Step 3 − After successful execution of the above step, you will receive the following output −
userregistration
userregistration
Step 4 − Copy the following code in
app/Http/Controllers/UserRegistration.php controller.
app/Http/Controllers/UserRegistration.php
Step 5 − Add the following line in app/Http/routes.php file.
app/Http/routes.php
Step 6 − Visit the following URL and you will see the registration form as shown in the below figure. Type the registration details and click Register and you will see on the second page that we have retrieved and displayed the user registration details.
Step 7 − The output will look something like as shown in below the following images.
registration
registration

Popular posts from this blog

What is Lumen?

What is Lumen? Lumen  is PHP micro-framework that built on Laravel’s top components.It is created by Taylor Otwell. It is perfect option for building Laravel based micro-services and fast REST API’s. It’s one of the fastest micro-frameworks available. You can install Lumen using composer by running below command 1 composer create - project -- prefer - dist laravel / lumen blog

Laravel – Response

Laravel – Response A web application responds to a user’s request in many ways depending on many parameters. This chapter explains you in detail about responses in Laravel web applications. Basic Response Laravel provides several different ways to return response. Response can be sent either from route or from controller. The basic response that can be sent is simple string as shown in the below sample code. This string will be automatically converted to appropriate HTTP response. Example Step 1  − Add the following code to  app/Http/routes.php  file. app/Http/routes.php 1 2 3 Route :: get ( '/basic_response' , function ( ) {    return 'Hello World' ; } ) ; Step 2  −  Visit  the following URL to test the basic response. 1 http : //localhost:8000/basic_response Step 3  − The output will appear as shown in the following image. Attaching Headers The response can be attached t...

Which template engine Laravel use ?

Which template engine Laravel use ? Laravel uses Blade Templating Engine. Blade is the simple, yet powerful templating engine provided with Laravel. Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views. In fact, all Blade views are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application. Blade view files use the .blade.php file extension and are typically stored in the resources/views directory.