skip to Main Content
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


  1. 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 a Name 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 model Name using that attribute

    // NameController
    
    public function click(Request $request, Name $name)
    {
      // Laravel will inject the Name model in $name automatically based on the slug
      // If no Name matched the slug then the controller will return 404
    }
    
    Login or Signup to reply.
  2. In 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:

    Route::get('articles/{title:slug}', 'ArticleController@show');
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search