How to get full URL to display in laravel

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

How to Get the Full URL in Laravel: A Developer's Guide for Beginners

Welcome to the world of Laravel! As you start building applications, understanding how to interact with the incoming HTTP request is fundamental. One common question beginners ask is: "How do I get the current URL inside my application?"

While getting the full URL isn't always necessary for displaying content directly, understanding how Laravel handles the request and provides access to URL components is a crucial skill. As a senior developer, I can guide you through the correct methods and best practices for accessing URL data in your Laravel projects.

Understanding the Laravel Request Object

In Laravel, all information about the incoming HTTP request—including headers, query strings, path, and full URL—is encapsulated within the Illuminate\Http\Request object. This object is typically injected into your Controller methods, making it the central hub for accessing request data.

The methods you mentioned are indeed the correct tools for dissecting a URL:

  • $request->fullUrl(): Returns the complete URL as requested by the browser (e.g., http://example.com/path?query=value). This is useful when you need to log or redirect based on the exact address the user typed.
  • $request->path(): Returns only the path portion of the URL, excluding the domain and query string (e.g., /path/to/resource).
  • $request->scheme(): Returns the protocol (e.g., http or https).
  • $request->host(): Returns the domain name (e.g., example.com).

Implementation: Accessing URL Data in Controllers

The key to implementing this is ensuring your route points to a controller method where you can access the Request object. This process happens entirely within your Controller layer, not directly within the route definition itself.

Here is a practical example demonstrating how to use these methods inside a controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UrlController extends Controller
{
    /**
     * Display information about the current request URL.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function showCurrentUrl(Request $request)
    {
        // 1. Get the full URL
        $fullUrl = $request->fullUrl();

        // 2. Get just the path
        $path = $request->path();

        // 3. Get the host (domain)
        $host = $request->host();

        return view('url_display', [
            'full' => $fullUrl,
            'path' => $path,
            'host' => $host,
        ]);
    }
}

Setting Up the Route

To make this accessible, you must define a route that points to the method above. This is done in your Routes file (usually routes/web.php).

// routes/web.php

use App\Http\Controllers\UrlController;
use Illuminate\Support\Facades\Route;

Route::get('/show-url', [UrlController::class, 'showCurrentUrl']);

Best Practices and Context

When working with URLs in Laravel, remember that the goal is often not just to read the URL, but to generate or manipulate it. If you need to create a link to another page, use the route() helper instead of manually constructing URLs. This keeps your application robust and easily adaptable if you ever change your domain structure—a core principle of good framework design, much like the principles Laravel promotes for building scalable applications on laravelcompany.com.

For displaying content dynamically based on the URL parameters (like an ID in /posts/1), you should use route model binding or simple parameter passing rather than relying solely on parsing the full URL string within your view.

Conclusion

To summarize, to get the full URL and its components in Laravel:

  1. Route Definition: Define a standard GET route pointing to a controller method (in routes/web.php).
  2. Controller Logic: Inject the Request object into your controller method signature (public function myMethod(Request $request)).
  3. Access Data: Use methods like $request->fullUrl(), $request->path(), and $request->host() inside your controller to retrieve the necessary URL information.

By sticking to these structured approaches, you ensure your code is clean, maintainable, and adheres to Laravel's conventions. Happy coding!