Javatpoint Logo
Javatpoint Logo

Laravel Routing Parameters

There are two types of parameters we can use:

  • Required Parameters
  • Optional Parameters
Laravel Routing Parameters

Required Parameters

The required parameters are the parameters that we pass in the URL. Sometimes you want to capture some segments of the URI then this can be done by passing the parameters to the URL. For example, you want to capture the user id from the URL.

Let's see the example without route parameters.

Output

When we enter the URL "localhost/laravelproject/public/".

Laravel Routing Parameters

When we enter the URL "localhost/laravelproject/public/about".

Laravel Routing Parameters

When we enter the URL "localhost/laravelproject/public/contact".

Laravel Routing Parameters

Let's see the example with route parameters.

The route parameters are enclosed within {} brackets, and parameters must contain alphabetic characters. It should not contain '-' character, and instead of using this character, you can use '_' character.

Route parameters are available in the route callbacks. Syntax of route parameters is given below:

Name of the callback/controller arguments

Where controller arguments are the route parameters.

Output

Laravel Routing Parameters

Let's see the example with multiple route parameters.

Output

Laravel Routing Parameters

Optional Parameters

Suppose you want to specify the route parameter occasionally, in order to achieve this, you can make the route parameter optional. To make the route parameter optional, you can place '?' operator after the parameter name. If you want to provide the optional parameter, and then make sure that you have also provided the default value to the variable.

Let's understand through some examples.

Example 1:

When we do not pass any variable to the URL, then the output would be:

Laravel Routing Parameters

When we pass 'akshita' in the URL, then the output would be:

Laravel Routing Parameters

From the above outputs, we observe that the parameter we pass in the URL is optional. As we have provided the default value to the parameter as Null, so if we do not pass any parameter, it will return null. If we pass the parameter in the URL, then the value of the parameter would be displayed.

Example 2:

In the above example, we have provided the default value as 'himani'.

Output

Laravel Routing Parameters

In the above example, we do not pass any parameter, so the default value is returned.

Laravel Routing Parameters

Regular Expression Constraints

These are the constraints that can format the route parameters by using the where method on a route instance. The 'where' method accepts the name of the parameter and regular expression constraint that defines how the parameter should be constrained.

Let's understand through some examples.

Example 1:

Suppose we want to pass the user name as a route parameter that contains only alphabetical characters.


Laravel Routing Parameters

Example 2:

Let's consider an example that accepts only numeric values.


Laravel Routing Parameters
Laravel Routing Parameters

Example 3:

Let's consider an example that accepts alphanumeric characters.


Laravel Routing Parameters

Global Constraints

You always want a route parameter to be constrained by a regular expression; then you can use the pattern method. You can define these patterns in the boot method of your RouteServiceProvider.

Global Constraints are used when we have multiple routes, and the same constraints are applied to all the routes. In Global Constraints, we do not have to apply the constraints individually to each route using where clause, we just need to define the pattern inside the boot() method, and it will be applied to all the routes.

Let's understand this through an example.

Step 1: Add the pattern in the boot method of RouteServiceProvider.php file.

Step 2: Add the routes in web.php file.

Output

When we pass the route parameter to the '/user' URL, then the output would be:

Laravel Routing Parameters

When we pass the route parameter to the '/post' URL, then the output would be:

Laravel Routing Parameters




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA