Laravel Routing ParametersThere are two types of parameters we can use:
Required ParametersThe 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/". When we enter the URL "localhost/laravelproject/public/about". When we enter the URL "localhost/laravelproject/public/contact". 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 Let's see the example with multiple route parameters. Output Optional ParametersSuppose 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: When we pass 'akshita' in the URL, then the output would be: 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 In the above example, we do not pass any parameter, so the default value is returned. Regular Expression ConstraintsThese 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. Example 2: Let's consider an example that accepts only numeric values. Example 3: Let's consider an example that accepts alphanumeric characters. Global ConstraintsYou 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: When we pass the route parameter to the '/post' URL, then the output would be: Next TopicLaravel Named Routes |