Skip to main content

Laravel URL Showcase

Discover real-world examples and use cases of Laravel URL in action. See how different applications leverage URL and slug management to create SEO-friendly, maintainable routing systems.

Understanding URL Components

Laravel URL provides powerful features for managing URLs and slugs:

  • HasUrl Trait: Core trait that adds URL management to any Eloquent model
  • UrlContract Interface: Defines how models compute their full URLs
  • Automatic Versioning: Track complete URL history for SEO redirects
  • Conflict Detection: Global uniqueness enforcement for active URLs
  • Cascading Updates: Automatically update child URLs when parents change
  • Fallback Routing: Smart route resolution with automatic 301 redirects

E-Commerce Product Catalog

Hierarchical Product URLs

Category-based product URLs with automatic updates

Overview

E-commerce platforms need hierarchical URLs like /shop/category/product-slug. When a category slug changes, all product URLs must update automatically. Laravel URL handles this with cascading updates, ensuring your URL structure stays consistent while preserving old URLs for SEO-friendly 301 redirects.

Key Features
  • Automatic URL versioning
  • Cascading URL updates
  • SEO-friendly redirects
  • Conflict detection
  • Collection-based slugs
Business Benefits
  • Improved SEO rankings
  • Better user experience
  • Automatic URL maintenance
  • Preserved link equity
  • Reduced manual work

Implementation Example

class Category extends Model implements UrlContract
{
use HasUrl;

public function products()
{
return $this->hasMany(Product::class);
}

public function getFullUrl(): string
{
return '/shop/' . ($this->slug ?? 'uncategorized');
}

public function getUrlDescendants(): iterable
{
return $this->products;
}
}

class Product extends Model implements UrlContract
{
use HasUrl;

public function category()
{
return $this->belongsTo(Category::class);
}

public function getFullUrl(): string
{
$categorySlug = $this->category->slug ?? 'uncategorized';
return "/shop/{$categorySlug}/{$this->slug}";
}
}

// Create category and product
$category = Category::create(['name' => 'Laptops']);
$category->dispatchSlug('laptops', 'categories');

$product = Product::create(['name' => 'MacBook Pro 14']);
$product->dispatchSlug('macbook-pro-14', 'products');

// Product URL: /shop/laptops/macbook-pro-14

// Change category slug
$category->dispatchSlug('computers', 'categories');
// Product URL automatically updates to: /shop/computers/macbook-pro-14
// Old URL (/shop/laptops/macbook-pro-14) redirects with 301

Content Management System (CMS)

Dynamic Page Routing

CMS pages with automatic URL management

Overview

Content management systems need flexible URL structures for pages, blog posts, and articles. Laravel URL enables dynamic routing where any URL can resolve to any model type. Combined with the fallback route system, you can create a CMS where content editors manage URLs through the admin panel, and the system automatically handles routing and redirects.

Use Cases
  • Blog posts & articles
  • Static pages
  • Landing pages
  • News articles
  • Documentation pages
Benefits
  • Flexible URL structure
  • Automatic redirects
  • SEO preservation
  • Easy content management
  • Version history

Implementation Example

class Page extends Model implements UrlContract
{
use HasUrl;

public function getFullUrl(): string
{
$parent = $this->parent;
if ($parent) {
return $parent->getActiveFullUrl() . '/' . ($this->slug ?? 'page-' . $this->id);
}
return '/' . ($this->slug ?? 'page-' . $this->id);
}

public function getUrlDescendants(): iterable
{
return $this->children;
}
}

// Create pages
$home = Page::create(['title' => 'Home']);
$home->dispatchSlug('', 'pages');

$about = Page::create(['title' => 'About Us', 'parent_id' => $home->id]);
$about->dispatchSlug('about', 'pages');

// URL: /about

// Use fallback route to resolve
Event::listen(UrlMatched::class, function (UrlMatched $event) {
if ($event->urlable instanceof Page) {
$event->respond(view('pages.show', ['page' => $event->urlable]));
}
});

Blog Platform

SEO-Optimized Blog URLs

Blog posts with category-based URLs and automatic redirects

Overview

Blog platforms need clean, SEO-friendly URLs that reflect content hierarchy. When a post category changes or a slug is updated, old URLs must redirect to new ones to preserve SEO value. Laravel URL handles this automatically, ensuring your blog maintains excellent SEO while allowing content editors to update URLs as needed.

Features
  • Category-based URLs
  • Automatic slug normalization
  • 301 redirects for old URLs
  • URL version history
  • Collection organization
SEO Benefits
  • Preserved link equity
  • Clean URL structure
  • Automatic redirects
  • URL history tracking
  • Better search rankings

Implementation Example

class Category extends Model implements UrlContract
{
use HasUrl;

public function posts()
{
return $this->hasMany(Post::class);
}

public function getFullUrl(): string
{
return '/blog/' . ($this->slug ?? 'uncategorized');
}

public function getUrlDescendants(): iterable
{
return $this->posts;
}
}

class Post extends Model implements UrlContract
{
use HasUrl;

public function category()
{
return $this->belongsTo(Category::class);
}

public function getFullUrl(): string
{
$categorySlug = $this->category->slug ?? 'uncategorized';
$postSlug = $this->slug ?? 'post-' . $this->id;
return "/blog/{$categorySlug}/{$postSlug}";
}
}

// Create post
$post = Post::create(['title' => 'Getting Started with Laravel']);
$post->dispatchSlug('getting-started-with-laravel', 'posts');

// URL: /blog/laravel/getting-started-with-laravel

// Update slug
$post->dispatchSlug('laravel-beginners-guide', 'posts');
// New URL: /blog/laravel/laravel-beginners-guide
// Old URL redirects with 301

Get Started

Ready to implement URL management in your application? Here's how to get started: