Getting the lastest slug from url in Laravel

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Getting the Latest Slug from a URL in Laravel: A Developer's Guide

Dealing with dynamic routes in web applications is a common scenario in Laravel development. When you use route parameters like {slug?}/{slug2?}/{slug3?}, determining which segment is the "last" one—the actual slug you are looking for—requires careful handling of the incoming request data. As a senior developer, we aim for solutions that are not only functional but also robust and maintainable.

This post will explore several effective ways to reliably extract the final slug from any complex URL structure in Laravel, moving beyond simple parameter access to a more generalized approach.

The Challenge with Dynamic Route Parameters

You have set up a route like this:

Route::get('{slug?}/{slug2?}/{slug3?}', 'PageController@show');

When a request hits this route, you need a reliable way to access the value corresponding to {slug3?} regardless of whether the URL has one segment (/my-slug) or three segments (/base/post/my-slug). Simply accessing request()->route('slug3') works perfectly if the parameter exists. However, if you need a dynamic method that doesn't rely on knowing the exact name of the final segment beforehand, we need a different strategy.

Method 1: Directly Accessing Route Parameters (The Standard Approach)

For simple cases where you know the specific route parameters defined in your file, accessing them directly via the Route facade or the Request object is the most idiomatic Laravel way. If you are certain that the final segment will always map to a known parameter name, this is the cleanest solution.

In your controller method:

use Illuminate\Http\Request;

class PageController extends Controller
{
    public function show(Request $request)
    {
        // Assuming the route definition was {slug}/{slug2}/{slug3}
        $slug3 = $request->route('slug3');

        if ($slug3) {
            return "The final slug is: " . $slug3;
        }

        return "Slug parameter not found.";
    }
}

Why this works: Laravel maps the URI segments directly to these named parameters. This is efficient and leverages the framework's built-in routing system. For more complex route definitions or when dealing with nested resources, understanding how Laravel handles request resolution is key, as discussed in documentation like that found on laravelcompany.com.

Method 2: Parsing the URI Path (The Robust Solution)

If your requirement is to always get the very last segment of the URL path, regardless of how many segments precede it, parsing the request URI itself offers maximum flexibility. This method treats the entire path as a string and splits it into an array.

You can access the full path via $request->path(). Then, you split it by the forward slash (/) and retrieve the last element.

use Illuminate\Http\Request;

class PageController extends Controller
{
    public function show(Request $request)
    {
        $fullPath = $request->path(); // e.g., "/base_url/slug/slug2/slug3"
        
        // Split the path by '/' and filter out any empty strings resulting from leading/trailing slashes
        $segments = array_filter(explode('/', $fullPath));

        if (empty($segments)) {
            return "No segments found.";
        }

        // The last element in the array is the final slug
        $lastSlug = end($segments); // or $segments[count($segments) - 1];

        return "The dynamically extracted last slug is: " . $lastSlug;
    }
}

Advantages of this method: This approach decouples your logic from the specific route parameter names. If you change your route structure tomorrow, as long as the final segment remains the desired value, your controller code remains resilient. This pattern promotes better decoupling between the presentation layer (URL) and the business logic.

Conclusion

For extracting dynamic data from Laravel routes, the choice depends on your context. If you are working within a strictly defined set of route parameters, Method 1 (Direct Access) is the simplest and most performant option. However, for robust applications where URLs might evolve or where you need to extract arbitrary path components, Method 2 (Parsing the URI Path) provides the necessary flexibility and resilience. Always favor solutions that align with Laravel's philosophy of clean, expressive code, ensuring your application remains scalable and maintainable.