responsive-sk/slim4-paths
最新稳定版本:6.1.0
Composer 安装命令:
composer require responsive-sk/slim4-paths
包简介
Complete path management solution for PHP applications with zero dependencies, built-in filesystem operations, enhanced security, framework presets (Laravel, Slim4, Mezzio), and comprehensive validation
关键字:
README 文档
README
A comprehensive, secure path management package for PHP applications with advanced features for modern web development.
Features
- Framework Presets - Laravel, Slim 4, Mezzio/Laminas presets
- Secure Path Joining - Prevents path traversal attacks
- 50+ Predefined Paths - Framework-specific directory structures
- Cross-Platform Compatibility - Works on Windows, Linux, macOS
- Auto-Directory Creation - Automatic directory structure setup
- Security Validation - Input validation and path sanitization
- Framework Agnostic - Works with any PHP framework
- Orbit CMS Support - Built-in support for content management
- Module System Integration - Advanced module path resolution
Installation
composer require responsive-sk/slim4-paths
Framework Presets
NEW in v3.0 Use framework-specific directory presets for instant setup:
Laravel Preset
use ResponsiveSk\Slim4Paths\Paths; $paths = Paths::withPreset('laravel', __DIR__); // Laravel-specific paths echo $paths->get('app'); // /path/to/app echo $paths->get('controllers'); // /path/to/app/Http/Controllers echo $paths->get('models'); // /path/to/app/Models echo $paths->get('views'); // /path/to/resources/views echo $paths->get('storage'); // /path/to/storage echo $paths->get('migrations'); // /path/to/database/migrations echo $paths->get('uploads'); // /path/to/storage/app/public
Slim 4 Preset
$paths = Paths::withPreset('slim4', __DIR__); // Slim 4-specific paths echo $paths->get('src'); // /path/to/src echo $paths->get('handlers'); // /path/to/src/Handler echo $paths->get('actions'); // /path/to/src/Action echo $paths->get('templates'); // /path/to/templates echo $paths->get('cache'); // /path/to/var/cache echo $paths->get('logs'); // /path/to/var/log echo $paths->get('uploads'); // /path/to/var/uploads
Mezzio/Laminas Preset
$paths = Paths::withPreset('mezzio', __DIR__); // or $paths = Paths::withPreset('laminas', __DIR__); // Mezzio-specific paths echo $paths->get('src'); // /path/to/src echo $paths->get('handlers'); // /path/to/src/Handler echo $paths->get('modules'); // /path/to/modules echo $paths->get('data'); // /path/to/data echo $paths->get('content'); // /path/to/content echo $paths->get('database'); // /path/to/data/database
Available Presets
// Get all available presets $presets = Paths::getAvailablePresets(); // ['laravel', 'slim4', 'mezzio', 'laminas'] // Get preset information $info = Paths::getPresetInfo(); foreach ($info as $key => $preset) { echo "{$key}: {$preset['name']} - {$preset['description']}\n"; }
Basic Usage
use ResponsiveSk\Slim4Paths\Paths; // Initialize with base path and configuration $paths = new Paths('/path/to/project', [ 'templates' => '/path/to/project/templates', 'content' => '/path/to/project/content', // ... more paths ]); // Basic path access $templatePath = $paths->templates(); $configPath = $paths->config(); $publicPath = $paths->public();
Convenience Methods
Paths::fromHere(__DIR__, $levelsUp = 3)
Convenience method to create a Paths instance from the current file location. Useful in modular systems.
use ResponsiveSk\Slim4Paths\Paths; // From a file at src/Modules/Core/SomeClass.php, go up 3 levels to project root $paths = Paths::fromHere(__DIR__, 3); // From a file at src/Services/SomeService.php, go up 2 levels to project root $paths = Paths::fromHere(__DIR__, 2); // Use resolved paths $dbPath = $paths->storage('database.db'); $logPath = $paths->logs('app.log');
Benefits:
- ✅ More expressive than manual path construction
- ✅ Less error-prone than counting
../manually - ✅ Works in tests, CLI, without DI containers
- ✅ Automatic path validation with clear error messages
Paths::fromEnv($envVar = 'APP_BASE_PATH')
Create a Paths instance from an environment variable. Useful for applications that need environment-based configuration.
// Set environment variable putenv('APP_BASE_PATH=/path/to/project'); // Create Paths instance from environment $paths = Paths::fromEnv(); // Or use custom environment variable putenv('BASE_PATH=/custom/path'); $paths = Paths::fromEnv('BASE_PATH');
Environment-based Usage
Combine environment-based initialization with existing path methods:
// Environment-based initialization $paths = Paths::fromEnv(); // or Paths::fromHere(__DIR__, 3) // Use existing path methods $paths->base(); // Project root: /path/to/project $paths->public(); // Public directory: /path/to/project/public $paths->config(); // Config directory: /path/to/project/config $paths->src(); // Source directory: /path/to/project/src // Configured paths work as expected $paths->storage('database.db'); // /path/to/project/var/storage/database.db $paths->logs('app.log'); // /path/to/project/var/logs/app.log
Core Path Methods
Basic Directories
$paths->base() // Project root directory $paths->config() // Configuration directory $paths->src() // Source code directory $paths->public() // Public web directory $paths->var() // Variable/runtime directory $paths->vendor() // Vendor directory
Template System
$paths->templates() // Main templates directory $paths->views() // Views directory $paths->layouts('main.php') // Layout templates $paths->partials('header.php') // Partial templates
Content Management (Orbit CMS)
$paths->content() // Content root directory $paths->articles('post.md') // Article files $paths->orbit() // Orbit database directory $paths->orbitDatabase('app') // Orbit database file (app.db)
Module System
$paths->moduleConfig('Core/Storage') // Module config file $paths->moduleRoutes('Core/Template') // Module routes file $paths->moduleTemplates('Optional/Blog') // Module templates directory
Asset Management
$paths->css('app.css') // CSS files $paths->js('main.js') // JavaScript files $paths->images('logo.png') // Image files $paths->fonts('roboto.woff2') // Font files $paths->media('video.mp4') // Media files
Development Tools
$paths->docs('api.md') // Documentation files $paths->scripts('deploy.sh') // Script files $paths->bin('console') // Executable files $paths->tests('UserTest.php') // Test files
Security & Storage
$paths->keys('private.key') // Security keys $paths->exports('data.json') // Export files $paths->imports('import.csv') // Import files $paths->logs('app.log') // Log files $paths->cache('views') // Cache files $paths->storage('uploads') // Storage files
Localization
$paths->lang('en', 'messages.php') // Language files $paths->translations('app.po') // Translation files $paths->locales() // Locales directory
Secure Path Joining
The getPath() method provides secure path joining with built-in protection against path traversal attacks:
// Secure path joining $safePath = $paths->getPath($baseDir, $relativePath); // Automatic validation try { $maliciousPath = $paths->getPath('/safe/dir', '../../../etc/passwd'); } catch (InvalidArgumentException $e) { // Path traversal detected and blocked echo $e->getMessage(); // "Path traversal detected in: ../../../etc/passwd" }
Security Features
- Path Traversal Protection - Detects and blocks
..sequences - Home Directory Protection - Blocks
~access - Input Validation - Validates all path components
- Cross-Platform Safety - Handles different directory separators
Configuration
Basic Configuration
$config = [ 'base_path' => '/path/to/project', 'paths' => [ 'templates' => '/path/to/project/templates', 'content' => '/path/to/project/content', 'public' => '/path/to/project/public', // ... more paths ] ]; $paths = new Paths($config['base_path'], $config['paths']);
Framework Integration
Slim Framework
use ResponsiveSk\Slim4Paths\Paths; use Slim\Factory\AppFactory; $app = AppFactory::create(); // Add Paths to container $container = $app->getContainer(); $container->set(Paths::class, function() { $config = require 'config/paths.php'; return new Paths($config['base_path'], $config['paths']); }); // Use in routes $app->get('/template/{name}', function ($request, $response, $args) { $paths = $this->get(Paths::class); $templatePath = $paths->templates($args['name'] . '.php'); if (!file_exists($templatePath)) { throw new \Slim\Exception\HttpNotFoundException($request); } // Render template... });
Orbit CMS Integration
This package includes built-in support for Orbit-style content management:
// Article management $articlePath = $paths->articles('getting-started.md'); $articlesDir = $paths->articles(); // Database management $appDb = $paths->orbitDatabase('app'); // var/orbit/app.db $markDb = $paths->orbitDatabase('mark'); // var/orbit/mark.db $cacheDb = $paths->orbitDatabase('cache'); // var/orbit/cache.db // Content organization $contentRoot = $paths->content(); // content/ $docsContent = $paths->content('docs'); // content/docs/
Module System Support
Advanced module path resolution for modular applications:
// Module configuration $storageConfig = $paths->moduleConfig('Core/Storage'); // Result: src/Modules/Core/Storage/config.php // Module routes $templateRoutes = $paths->moduleRoutes('Core/Template'); // Result: src/Modules/Core/Template/routes.php // Module templates $blogTemplates = $paths->moduleTemplates('Optional/Blog'); // Result: src/Modules/Optional/Blog/templates/ $blogArticleTemplate = $paths->moduleTemplates('Optional/Blog', 'article.php'); // Result: src/Modules/Optional/Blog/templates/article.php
Security Best Practices
Always Use Secure Path Joining
// Good - secure path joining $filePath = $paths->getPath($baseDir, $userInput); // Bad - vulnerable to path traversal $filePath = $baseDir . '/' . $userInput;
Validate User Input
function loadUserFile(Paths $paths, string $filename) { // Validate filename if (!preg_match('/^[a-zA-Z0-9_-]+\.txt$/', $filename)) { throw new InvalidArgumentException('Invalid filename'); } // Use secure path joining $filePath = $paths->getPath($paths->uploads(), $filename); return file_get_contents($filePath); }
Directory Traversal Protection
The package automatically protects against common attacks:
// These will throw InvalidArgumentException $paths->getPath('/safe/dir', '../../../etc/passwd'); $paths->getPath('/safe/dir', '~/sensitive/file'); $paths->getPath('/safe/dir', 'file/../../../etc/passwd');
Testing
use PHPUnit\Framework\TestCase; use ResponsiveSk\Slim4Paths\Paths; class PathsTest extends TestCase { public function testSecurePathJoining() { $paths = new Paths('/project', ['uploads' => '/project/uploads']); // Valid path $validPath = $paths->getPath($paths->uploads(), 'file.txt'); $this->assertEquals('/project/uploads/file.txt', $validPath); // Invalid path should throw exception $this->expectException(InvalidArgumentException::class); $paths->getPath($paths->uploads(), '../../../etc/passwd'); } }
Requirements
- PHP 8.1 or higher
- No additional dependencies
License
MIT License
Contributing
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
Changelog
Version 2.0.0
- Added 30+ predefined path methods
- Enhanced security with path traversal protection
- Added Orbit CMS support
- Added module system integration
- Added comprehensive test coverage
- Breaking changes from 1.x (see migration guide)
Version 1.0.0
- Initial release
- Basic path management functionality
responsive-sk/slim4-paths 适用场景与选型建议
responsive-sk/slim4-paths 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 801 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 05 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「php」 「filesystem」 「security」 「cms」 「validation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 responsive-sk/slim4-paths 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 responsive-sk/slim4-paths 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 responsive-sk/slim4-paths 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
A SDK for working with B2 cloud storage.
A PHP class providing static methods for reading, writing, copying, moving, and deleting files and directories, MIME type detection, image size detection, and file permission management
The flysystem adapter for yandex disk rest api.
A sleek PHP wrapper around rclone with Laravel-style fluent API syntax
phpABLE file-system library
统计信息
- 总下载量: 801
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 28
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-05-24