Download the PHP package responsive-sk/slim4-root without Composer
On this page you can find all versions of the php package responsive-sk/slim4-root. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download responsive-sk/slim4-root
More information about responsive-sk/slim4-root
Files in responsive-sk/slim4-root
Package slim4-root
Short Description Root path management with auto-discovery, base path detection and testing utilities for Slim 4 applications
License MIT
Informations about the package slim4-root
Slim4 Root
Root path management with auto-discovery and base path detection for Slim 4 applications. Say goodbye to relative paths like __DIR__ . '/../../../config' and hello to clean, consistent path access.
New in 1.4
- Automatic root path discovery for common directory structures
- Built-in path validation with detailed error messages
- Cross-platform path normalization for Windows and Unix
- Dedicated exception handling for path-related errors
- Enhanced developer experience with more intuitive API
- No more relative paths with
../- everything is relative to the root - Base path detection for applications running in subdirectories
- Testing utilities for easier test setup and execution
- Monolog integration for easy logging setup
Features
- Centralized path management for Slim 4 applications
- Auto-discovery of common directory structures
- Support for custom directory structures
- Path validation and normalization
- Middleware for accessing paths in route handlers
- Base path detection for applications running in subdirectories
- Testing utilities for easier test setup and execution
- Monolog integration for easy logging setup
- PSR-11 container integration
- No dependencies (except Slim 4 and PSR Container)
- Fully tested
- Ready for PHP 7.4 and 8.0+
Requirements
- PHP 7.4+ or 8.0+
- Slim 4
Installation
Usage
Basic Usage
With Custom Paths
Path Auto-Discovery
Path Validation
Path Normalization
With DI Container
With Paths Middleware
With Base Path Detection
Integration with Twig
Integration with Monolog
Or use the MonologProvider for easier setup:
For more details, see Monolog Integration.
Testing Utilities
The package includes a Testing module that provides utilities for easier test setup and execution.
TestContainer
TestContainer is a simple static container for sharing objects between tests without using globals.
Bootstrap File
The package includes a bootstrap file that you can use in your PHPUnit configuration:
Or you can create your own bootstrap file:
Available Methods
PathsInterface
getRootPath()- Get the root path of the projectgetConfigPath()- Get the config pathgetResourcesPath()- Get the resources pathgetViewsPath()- Get the views pathgetAssetsPath()- Get the assets pathgetCachePath()- Get the cache pathgetLogsPath()- Get the logs pathgetPublicPath()- Get the public pathgetDatabasePath()- Get the database pathgetMigrationsPath()- Get the migrations pathgetStoragePath()- Get the storage pathgetTestsPath()- Get the tests pathpath(string $path)- Get a path relative to the root pathgetPaths()- Get all paths as an associative array
BasePathMiddleware
process(ServerRequestInterface $request, RequestHandlerInterface $handler)- Process the request and set the base path for the Slim application
PathsDiscoverer
discover(string $rootPath)- Discover paths in the given root path
PathsValidator
validate(array $paths, bool $strict)- Validate paths
PathsNormalizer
normalize(string $path)- Normalize path
TestContainer
set(string $key, mixed $value)- Set an item in the containerget(string $key, mixed $default = null)- Get an item from the containerhas(string $key)- Check if an item exists in the containerremove(string $key)- Remove an item from the containerclear()- Clear all items from the container
MonologFactory
createLogger(array $config = [])- Create a new Monolog logger instancecreateConsoleLogger(string $name, Level $level)- Create a console loggercreateFileLogger(string $name, string $path, Level $level)- Create a file loggercreateRotatingLogger(string $name, string $path, int $maxFiles, Level $level)- Create a rotating file logger
MonologProvider
register(ContainerInterface $container, array $config)- Register a logger in the containerregisterMultiple(ContainerInterface $container, array $loggers)- Register multiple loggersregisterConsoleLogger(ContainerInterface $container, string $name, Level $level)- Register a console loggerregisterFileLogger(ContainerInterface $container, string $name, string $path, Level $level)- Register a file loggerregisterRotatingLogger(ContainerInterface $container, string $name, string $path, int $maxFiles, Level $level)- Register a rotating file logger
Customizing Paths
You can customize the paths by passing an array of custom paths to the constructor:
Feature Comparison
| Feature | v1.1 | v1.2 | v1.3 | v1.4 |
|---|---|---|---|---|
| Auto-discovery | ❌ No | ✅ Yes | ✅ Yes | ✅ Yes |
| Path validation | ❌ Basic | ✅ Comprehensive | ✅ Comprehensive | ✅ Comprehensive |
| Path normalization | ❌ No | ✅ Yes | ✅ Yes | ✅ Yes |
| Error handling | ❌ Generic exceptions | ✅ Dedicated exceptions | ✅ Dedicated exceptions | ✅ Dedicated exceptions |
| Relative paths | ❌ Manual ../ |
✅ Everything relative to root | ✅ Everything relative to root | ✅ Everything relative to root |
| Base path detection | ❌ No | ✅ Yes | ✅ Yes | ✅ Yes |
| Testing utilities | ❌ No | ❌ No | ✅ Basic | ✅ Enhanced |
| Monolog integration | ❌ No | ❌ No | ❌ No | ✅ Yes |
| Test coverage | ✅ Good | ✅ Excellent | ✅ Excellent | ✅ Excellent |
| Flexibility | ✅ Good | ✅ Excellent | ✅ Excellent | ✅ Excellent |
Auto-Discovery
The Paths class can automatically discover common directory structures in your project. This is enabled by default, but you can disable it by passing false as the third parameter to the constructor.
The auto-discovery process looks for the following directories:
config- Looks forconfig,app/config,etcresources- Looks forresources,app/resources,resviews- Looks forresources/views,templates,views,app/viewsassets- Looks forresources/assets,assets,public/assetscache- Looks forvar/cache,cache,tmp/cache,storage/cachelogs- Looks forvar/logs,logs,log,storage/logspublic- Looks forpublic,web,www,htdocsdatabase- Looks fordatabase,db,storage/databasemigrations- Looks fordatabase/migrations,migrations,db/migrationsstorage- Looks forstorage,var,datatests- Looks fortests,test
Path Validation
The Paths class can validate that all paths exist. This is disabled by default, but you can enable it by passing true as the fourth parameter to the constructor.
If validation is enabled and a path doesn't exist, an InvalidPathException will be thrown:
Testing
Documentation
For detailed documentation, see:
- Documentation (English)
- Dokumentácia (Slovenčina)
Examples
- Simple Example - Basic usage in a typical Slim 4 project
- Template Engines Integration - Integration with popular PHP template engines
- Advanced Use Cases - Detailed use cases for various scenarios and architectures
- Testing - Testing utilities and integration with PHPUnit
- Monolog Integration - Integration with Monolog for logging
License
The MIT License (MIT). Please see License File for more information.
Credits
- Responsive.sk Team
- All Contributors
About Responsive.sk
Responsive.sk is a web development company specializing in creating modern, responsive web applications using the latest technologies and best practices.
Roadmap
We're planning to expand this package with integrations for other frameworks and libraries. Check out our TODO list for upcoming features and ways to contribute.
Community
We're looking to collaborate with Laminas and Cycle ORM communities to create integrations for these frameworks. If you're interested in contributing, please check out our TODO list and get in touch!