Route::get('/click/{name:slug}', [NameController::class, 'click']);
What is {name:slug}? Is it constraint for parameter? How to use it? Is it build-in function?
I think slug is the simple url name. I want to understand it. Thanks.
Route::get('/click/{name:slug}', [NameController::class, 'click']);
What is {name:slug}? Is it constraint for parameter? How to use it? Is it build-in function?
I think slug is the simple url name. I want to understand it. Thanks.
2
Answers
Read more about laravel model binding, specifically customizing binding key.
Here’s a description for your code:
The
name
part refers to a model so in your code there should be aName
model, the:slug
part refers to the attribute of that model. Meaning when calling the endpoint/route/click/this-is-a-name-slug
it will query the modelName
using that attributeIn Laravel, the {name:slug} syntax in route parameters is used to define a named route parameter with a specific pattern called a "slug".
The {name:slug} route parameter allows you to define a dynamic part of the URL that matches the specified pattern. For example, consider the following route definition in Laravel:
In this example, {title:slug} is a route parameter named "title" with the "slug" pattern. It indicates that the value provided in the URL segment will be transformed into a slug format. The transformed value will be passed as an argument to the show method of the ArticleController.
For instance, if you access the URL /articles/my-example-article-title, the show method of the ArticleController will receive ‘my-example-article-title’ as the value of the title parameter.