Understanding Laravel Route Parameters

Today we’ll be looking at more of Laravel’s routing features. This time we’ll be dealing with how Laravel handles route parameters. We’ve gone over Laravel routing before, but this time we’ll be looking at more advanced scenarios.

Route Parameters

Laravel let’s us use route parameters in our routes. This helps when you want to create routes with things like a subcategory or a specific identifier (name, id, or any other parameter). Let’s look at the different ways to use route parameters.

Getting a Basic Route Parameter

In this example, we will have a route for users and we’ll pull the identifying parameter. We’ll do it two different ways with name and with an id



    // get the cuteness level of a puppy
    Route::get('puppies/{cutelevel}', function($cutelevel) 
    {
        return 'This puppy is an absolute ' . $cutelevel . ' out of ' . $cutelevel;
    });

    // OR

    // get the parameter of name
    Route::get('users/{name}', function($name) 
    {
        return 'User Name is ' . $name;
    });


Testing Cuteness Level: Now in our browser, if we access http://example.com/puppies/5, our browser would show This puppy is an absolute 5 out of 5.

Testing Name: Now in our browser, if we access http://example.com/users/chris, our browser would show User Name is Chris.

Using Optional Route Parameters

For this example, let’s say we have a gallery of photos. We also have categories of photos. The category will be optional, otherwise, we’ll just show all the photos.



    // optional category
    Route::get('gallery/{category?}', function($category) 
    {
        // if category is set, show the category
        // if not, then show all
        if ($category)
            return 'This is the ' . $category . ' section.';
        else 
            return 'These are all the photos.';

    });

Testing Optional Category: If we visit http://example.com/gallery/puppies, our browser would return This is the puppies section.

Testing No Optional Category: If we visit http://example.com/gallery, our browser will return These are all the photos.

Route Parameter Defaults

Let’s say you always want a user to have a category selected. So if they don’t have a category selected, they will automatically default to lets say the sunsets category.



    // optional category with a default
    Route::get('gallery/{category?}', function($category = 'sunsets')
    {
        return 'This is the ' . $category . ' category.';
    });

Testing No Category: If we visit http://example.com/gallery, then our browser will return This is the sunsets category.

Testing A Category: If we visit http://example.com/gallery/puppies, then our browser will return This is the puppies category.

Pulling Real Data

So we’ve gone through the basics of route parameters. Let’s talk about using these for a real world scenario. We will use the galleries example.

In the real world, you wouldn’t want to give your visitor a sentence just telling them that they are seeing the puppies category. Your user will want to see puppies!.

Let’s say you already set up your Laravel application, migrations, and database. We can use Eloquent to pull data based on our route parameters.

Once you also have your Eloquent model, you can call the information you need from the route (of course when your application gets larger, you’ll want to move this logic into a controller).



    // get the category of gallery for viewing
    Route::get('gallery/{category?}', function($category) {
        
        // get the gallery stuff for the category
        $gallery = Gallery::where('category', '=', $category);

        // return a view and send the gallery data to the view
        return View::make('gallery')
            ->with('gallery', $gallery);
    });

Conclusion

As you can see, it is very easy to use route parameters and the Eloquent ORM to pull real data and send it to your view.

Let us know if there are any specific application use cases you’d like to see and we’ll try to help out. You can expand on this with filters for authentication, route groups to prefix all routes, and much more.

I’d advise you to check out the official Laravel route parameter docs to do more things like route constraints using regular expressions and more.

Chris Sevilleja

Co-founder of Scotch.io. Slapping the keyboard until something good happens.