Laravel

How to Use Parameters in Laravel Routes

January 30, 2018

author:

How to Use Parameters in Laravel Routes

Moving a step further, you may realize that at times we need to capture some segments of the URI within our routes to render specific data. With Laravel, this is really easy!

Route Parameters Types in Laravel 5.6:

Laravel parameters can be matched by:

• Required Parameters
• Optional Parameters

# Required Parameters Template Routes

This parameter is used when we rigidly want a route to provide some arguments. For instance, we need to fetch user id or username from URL and process those parameters to render dynamic content. Here’s how we can do that:

# routes/web.php

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

How do we test this route?

http://localhost:8000/welcome/hashvel
Laravel Routing Templates and Layout

Try yourself, insert some random names. The idea is to store {username} which is passed as an argument in URL and store it in variable $username to process further and display in on screen. This is just a simple use case, you can tweak this logic as you want.

For example, we can add as many parameters as we require:

# routes/web.php

Route::get('courses/{language}/{topic}',function($language, $topic){
    return 'Hey! You are learning '. $topic .' in '. $language;
});

Just remember that route parameters should consist only alphabetic characters and must be enclosed within {} braces. Now test the above route:

http://localhost:8000/courses/laravel/routing
Laravel Multiple Routes

# Optional Parameters Template Routes

While at other times, you may occasionally want to add a route parameter, but make its presence optional. In such cases application doesn’t throw an error if it doesn’t encounter URL arguments. Cool, isn’t it? Here’s how we can do that.

Copy both the routes as shown here:

# routes/web.php

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

Route::get('users/{name?}', function ($name = 'Khushi') {
    return 'Name: '.$name;
});

You can now test the routes like:

# user/{username} routes:

http://localhost:8000/user
http://localhost:8000/user/hashvel

# users/{username} routes:

http://localhost:8000/users
http://localhost:8000/users/hashvel

Did you see the difference in both the routes? In the first route, there is no default constraint for the use case if an argument isn’t passed. While, in the second route, we did handle that smartly.

Also, routing resources can be used which sets the API routes automatically.

# Laravel Missing Parameter Issue

At times, Laravel beginners learn how to create routes in web.php file but do not know how how to pass routes from view (or blade) pages. So here’s how you can pass missing parameters in Laravel blade files:

<li><a href="{{ route('welcome', $user) }}">Welcome</a></li>

Conclusion:

Since route is the building block of any laravel application, in this post, we took the basic routes a little further and learned about routing parameters. In the coming posts, we will learn about controllers and views.

Leave a comment

Your email address will not be published. Required fields are marked *