Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Laravel Route Parameters With Slash

One epic thing with Laravel routing is the ability to quickly define routes with slash Parameters. What we mean by slash parameters is a URL that allows HTTP request like below –

localhost/myParam/myParam2

This type of routing is not only great for SEO but really good for having an understandable URL for users. Getting rid of query string parameters like localhost?myParam=myValue

Defining Slash Parameters in Laravel

You would imagine setting up this type of functionality would be quite tricky, but, as Laravel always proves, it’s a really development-speed-boosting framework. Slash parameters are defined with the following syntax –

{categoryName?}

Have a read through the following tutorial to see how this syntax fits into routes.

Let’s start

In this tutorial, we will define a GET route to accept two parameters which will have names defined just like an ordinary query string.

Pre notes

For a small bit of context, I have created a HomeController of which’s index function returns the default welcome view.

Step 1

Head over to your routes/web.php file.

Step 2

For the tutorial’s sake, we will edit the route for the root URL which returns the welcome view.

The route currently looks like this –

Route::get('/', 'HomeController@index');

Now we can add some parameters, these will be CategoryName and ProductId.

Edit the route to look like the following

Route::get('/{categoryName?}/{productId?}', 'HomeController@index');
Step 3

Now with the route set with parameters, we can go to any of the following variations of URL.

  • localhost/
  • localhost/Clothing
  • localhost/Clothing/4

Or similar.

Step 4

To test the values are being passed via the URL, head over to your HomeController or the controller that the route you edited is calling to.

Edit your controller to look something like the following –

public function index(Request $request)
    {

        if ($request->isMethod('GET')) {
            if (isset($request->categoryName)) {
                echo $request->categoryName . " ";
            }
            if (isset($request->productId)) {
                echo $request->productId;
            }
        }

        return view("welcome");
    }
Step 5

Now let’s go to URL of localhost/Clothing/5 and you will see that the parameters are printed to the page. Then, if you load just simply localhost/, no parameters will print!

Summary

Defining routes like this has endless variation and brilliant for dynamic web applications. As you can see from this tutorial, the layout of your routing file will totally depend on the type of application you are building.

If you’re interested in adding rules to your routes then check the following article I wrote earlier –  Adding regex rules to your dynamic routes, then check this article I written earlier.

The post Laravel Route Parameters With Slash appeared first on Code Wall.



This post first appeared on Code Wall - Web Development & Programming, please read the originial post: here

Share the post

Laravel Route Parameters With Slash

×

Subscribe to Code Wall - Web Development & Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×