The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

Larapath

Larapath stats

Downloads
3
Stars
2
Open Issues
1
Forks
0

View on GitHub →

A secure, fluent path builder for PHP with sanitization strategies and Laravel 11-12 integration

LaraPath

A secure, fluent path builder for PHP with sanitization strategies and Laravel 11-12 integration.

Features

  • πŸ”’ Security First: Built-in protection against directory traversal attacks
  • 🎯 Fluent API: Clean, readable path building with method chaining
  • πŸ”§ Sanitization Strategies: Multiple strategies for different use cases (hash, slug, snake_case, timestamp)
  • πŸš€ Immutable Operations: Thread-safe path building with no side effects
  • πŸ—οΈ Strategy Pattern: Extensible sanitization system
  • 🎨 Type Safety: Full type hints and IDE autocompletion
  • πŸ“¦ Laravel Integration: Optional Laravel Storage facade integration

Installation

composer require hdaklue/larapath

Quick Start

use Hdaklue\PathBuilder\PathBuilder;
use Hdaklue\PathBuilder\Enums\SanitizationStrategy;
// Or use the facade in Laravel
use LaraPath;
 
// Basic usage (static)
$path = PathBuilder::base('uploads')
->add('images')
->add('avatar.jpg')
->toString(); // "uploads/images/avatar.jpg"
 
// Laravel facade usage (auto-registered)
$path = LaraPath::base('uploads')
->add('images')
->add('avatar.jpg')
->toString();
 
// With sanitization strategies
$path = PathBuilder::base('uploads')
->add('user-123', SanitizationStrategy::HASHED)
->add('My File.jpg', SanitizationStrategy::SLUG)
->validate()
->toString(); // "uploads/a665a45920422f9d417e4867efdc4fb8/my-file.jpg"

Real-World Use Cases

For detailed examples of how LaraPath solves critical problems in multi-tenant Laravel applications, see Real World Problems.

The guide covers 5 essential scenarios:

  • Multi-tenant storage organization with privacy protection
  • Database-friendly storage strategies across multiple disks
  • File naming conflicts and data loss prevention
  • Cross-platform filename sanitization and migration
  • Team-wide consistency and standardization

Perfect for understanding LaraPath's value in complex Laravel applications.

Usage Examples

Sanitization Strategies

// Hash sensitive data
$path = PathBuilder::base('storage')
->add('user@email.com', SanitizationStrategy::HASHED)
->toString(); // "storage/5d41402abc4b2a76b9719d911017c592"
 
// Create URL-friendly names
$path = PathBuilder::base('uploads')
->add('My Amazing File!', SanitizationStrategy::SLUG)
->toString(); // "uploads/my-amazing-file"
 
// Convert to snake_case
$path = PathBuilder::base('files')
->add('CamelCase Name', SanitizationStrategy::SNAKE)
->toString(); // "files/camel_case_name"
 
// Add timestamps for uniqueness
$path = PathBuilder::base('temp')
->add('session', SanitizationStrategy::TIMESTAMP)
->toString(); // "temp/session_1640995200"

Path Operations

$builder = PathBuilder::base('files/video.mp4');
 
// Extract path components
$extension = $builder->getExtension(); // "mp4"
$filename = $builder->getFilename(); // "video.mp4"
$filenameWithoutExt = $builder->getFilenameWithoutExtension(); // "video"
$directory = $builder->getDirectoryPath(); // "files"
 
// Modify paths
$newPath = $builder->replaceExtension('webm')->toString(); // "files/video.webm"

Laravel Integration

LaraPath is automatically registered in Laravel applications with facade support and container binding.

// Using the facade (recommended for Laravel)
use LaraPath;
 
$exists = LaraPath::base('uploads')
->add('avatar.jpg')
->exists('public'); // Uses Storage::disk('public')->exists()
 
// Using container binding
$builder = app('larapath');
$size = $builder->base('files')
->add('document.pdf')
->size(); // Uses Storage::size()
 
// Using static methods (framework-agnostic)
use Hdaklue\PathBuilder\PathBuilder;
 
$url = PathBuilder::base('images')
->add('logo.png')
->url('public'); // Uses Storage::disk('public')->url()
 
// Delete file
$deleted = LaraPath::base('temp')
->add('cache.tmp')
->delete(); // Uses Storage::delete()

Validation and Security

// Automatic validation
$path = PathBuilder::base('uploads')
->add('../../../etc/passwd') // Dangerous path
->validate() // Throws InvalidArgumentException
->toString();
 
// Manual safety check
$isSafe = PathBuilder::isSafe('uploads/../dangerous/path'); // false

Custom Strategies

use Hdaklue\PathBuilder\Contracts\SanitizationStrategyContract;
 
class UuidStrategy implements SanitizationStrategyContract
{
public static function apply(string $input): string
{
return \Str::uuid()->toString();
}
}
 
// Use custom strategy
$path = PathBuilder::base('files')
->add('temp-file', UuidStrategy::class)
->toString(); // "files/550e8400-e29b-41d4-a716-446655440000"

API Reference

PathBuilder Methods

  • PathBuilder::base(string $path, ?SanitizationStrategy $strategy = null): self
  • add(string $name, ?SanitizationStrategy $strategy = null): self
  • addFile(string $filename, ?SanitizationStrategy $strategy = null): self
  • addTimestampedDir(): self
  • addHashedDir(string $input, string $algorithm = 'md5'): self
  • replaceExtension(string $newExt): self
  • getExtension(): string
  • getFilename(): string
  • getFilenameWithoutExtension(): string
  • getDirectoryPath(): string
  • ensureTrailing(): self
  • removeTrailing(): self
  • validate(): self
  • toString(): string

Laravel Integration Methods

  • mustExist(string $disk = 'local'): self
  • mustNotExist(string $disk = 'local'): self
  • exists(string $disk = 'local'): bool
  • size(string $disk = 'local'): int
  • url(string $disk = 'local'): string
  • delete(string $disk = 'local'): bool

Static Utility Methods

  • PathBuilder::build(array $segments): string
  • PathBuilder::join(string ...$segments): string
  • PathBuilder::normalize(string $path): string
  • PathBuilder::isSafe(string $path): bool
  • PathBuilder::buildRelativePath(string $absolutePath, string $basePath): string

Available Strategies

  • SanitizationStrategy::HASHED - MD5 hash of input
  • SanitizationStrategy::SLUG - URL-friendly slug
  • SanitizationStrategy::SNAKE - snake_case conversion
  • SanitizationStrategy::TIMESTAMP - Appends Unix timestamp

Requirements

  • PHP ^8.2
  • illuminate/support ^11.0|^12.0 (for Laravel integration)

Testing

composer test

License

MIT License. See LICENSE for details.

Contributing

Please see CONTRIBUTING.md for details.

Security

If you discover any security related issues, please email hassan@daklue.com instead of using the issue tracker.

hdaklue photo

Coder by night, marketer by day β€” I don’t switch hats, I merge them. I’m a Marketing Engineer. TALL Stack Enthusiast.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Hdaklue Larapath Related Articles

A Fluent Path Builder for PHP and Laravel image

A Fluent Path Builder for PHP and Laravel

Read article
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Tighten logo

Tighten

We help companies turn great ideas into amazing apps, products, and services.

Tighten
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Blastup logo

Blastup

Blastup provides social media enhancement services including buying Instagram likes, followers, and views, with features like instant delivery and a variety of packages to suit different needs.

Blastup
Securing Laravel logo

Securing Laravel

The essential security resource for Laravel devs, covering everything you need to keep your apps secure. Sign up to receive weekly security tips and monthly in depth articles, diving deep into security concepts you need to know!

Securing Laravel