#2 Laravel 6 Basics | Routing (II) | Route Parameters | Required | Optional | Regular Expression

preview_player
Показать описание
In Part-2 of Laravel 6 Basics, we will continue learn about Routing in detail.

We will keep experimenting with lot of route functionalities in new Laravel 6 website and in E-com series.

In this video, we will learn about Route Parameters includes Required Parameters, Optional Parameters and Regular Expression Constraints.

Required Parameters

In some routes, it is mandatory to send one or more parameter to function.

Suppose you create a user route with mandatory name parameter. In this case, you have to send name every time in URL otherwise error will come.

Route::get('user/{name}', function ($name) {
return 'User '.$name;
});

Optional Parameters

In some routes, it is optional to send one or more parameter to function.
If parameter is optional, you need to add ? mark after the parameter name.

Suppose you create a user route with optional name parameter. In this case, you can send name if required in URL and no error will come even if you will not send. You just need to assign null to parameter in function in such cases.

Route::get('user/{name?}', function ($name = null) {
return $name;
});

Regular Expression Constraints

In some routes, you need to pass specific parameters like all numbers or all characters. In such cases, we can use Regular Expression Constraints with routes as shown in video.

Global Constraints

If you want to globally fix regular expression for some specific routes with parameters then you can define in RouteServiceProvider file.

Once the pattern has been defined, it is automatically applied to all routes using that parameter name.

Encoded Forward Slashes

The Laravel routing component allows all characters except /. You must explicitly allow / to be part of your placeholder using a where condition regular expression as shown in video.

Join as premium to get E-commerce Website code in Laravel 6 / 7 / 8 :-
Рекомендации по теме
Комментарии
Автор

Its been very long time mate.. I check almost daily for this series

ahmadkakarr
Автор

Sir go advance with Apis in this series. ... ... an another suggestion. ..

MuhammadAdnan-gxrd
Автор

Sir rloes gates policies and much more kindly start as soon as possible practical example. ..

MuhammadAdnan-gxrd