mirror of
https://github.com/pacnpal/thrillwiki_laravel.git
synced 2025-12-20 15:51:09 -05:00
2.6 KiB
2.6 KiB
Slug History System
Overview
The slug history system provides a way to track changes to model slugs over time, ensuring that old URLs continue to work even after slugs are updated. This is particularly important for maintaining SEO value and preventing broken links.
Components
1. HasSlugHistory Trait
Located in app/Traits/HasSlugHistory.php
Key Features:
- Automatic slug generation from name field
- Tracking of slug changes
- Historical slug lookup
- Handling of duplicate slugs
- Polymorphic relationship with SlugHistory model
2. SlugHistory Model
Located in app/Models/SlugHistory.php
Purpose:
- Stores historical slugs for any model using HasSlugHistory trait
- Uses polymorphic relationships to link with parent models
- Maintains timestamps for tracking when slugs were used
Database Structure
CREATE TABLE slug_histories (
id bigint unsigned NOT NULL AUTO_INCREMENT,
sluggable_type varchar(255) NOT NULL,
sluggable_id bigint unsigned NOT NULL,
slug varchar(255) NOT NULL,
created_at timestamp NULL DEFAULT NULL,
updated_at timestamp NULL DEFAULT NULL,
PRIMARY KEY (id),
KEY idx_sluggable (sluggable_type, slug)
);
Usage Example
class Operator extends Model
{
use HasSlugHistory;
// Model implementation
}
// Finding by current or historical slug
$operator = Operator::findBySlug('old-slug');
$operator = Operator::findBySlugOrFail('old-slug');
Current Implementation
Models using the system:
- Operator (park operators)
- Manufacturer (ride manufacturers)
- Park (to be implemented)
- ParkArea (to be implemented)
Features
-
Automatic Slug Generation
- Converts name to URL-friendly format
- Handles duplicates by appending numbers
- Triggered on model creation
-
History Tracking
- Saves old slugs when updated
- Maintains chronological order
- Links to original model via polymorphic relationship
-
Slug Lookup
- Checks current slugs first
- Falls back to historical slugs
- Maintains efficient indexes for quick lookups
Benefits
-
SEO Friendly
- Maintains link equity
- Prevents 404 errors
- Supports URL structure changes
-
User Experience
- Old bookmarks continue to work
- Prevents broken links
- Transparent to end users
-
Performance
- Efficient database indexing
- Minimal overhead
- Cached lookups (to be implemented)
Future Enhancements
- Add caching layer for frequent lookups
- Implement automatic redirects with proper status codes
- Add slug cleanup for old, unused slugs
- Add analytics for tracking slug usage