To create a route with a wildcard parameter in Laravel, you can use the Route::get()
method with a placeholder inside curly braces {}
to define the wildcard parameter.
Here is an example:
Route::get('/users/{name}', function ($name) {
return "Hello, $name!";
});
In the above example, the route /users/{name}
will match any URL that starts with /users/
followed by a parameter. The parameter value will be passed to the closure function as an argument.
You can handle the wildcard parameter in the closure by defining a parameter with the same name. In this case, the parameter is $name
. You can use this parameter inside the closure to perform any desired logic.
You can also add constraints to a wildcard parameter by specifying a regular expression pattern. For example, you can restrict the name
parameter to only alphabetic characters using a regular expression pattern.
Route::get('/users/{name}', function ($name) {
return "Hello, $name!";
})->where('name', '[A-Za-z]+');
In the above code, the where()
method is used to specify the constraint for the name
parameter. The regular expression [A-Za-z]+
restricts the parameter to only alphabetic characters.
Note that you can use other HTTP methods such as post
, put
, patch
, or delete
instead of get
to create routes with wildcard parameters based on your requirements.