Add BasePathMiddleware for subdirectory deployments#3432
Open
KalimeroMK wants to merge 1 commit intoslimphp:4.xfrom
Open
Add BasePathMiddleware for subdirectory deployments#3432KalimeroMK wants to merge 1 commit intoslimphp:4.xfrom
KalimeroMK wants to merge 1 commit intoslimphp:4.xfrom
Conversation
- Adds middleware to strip base path from request URI - Supports explicit path via constructor - Supports auto-detection via BasePathMiddleware::fromRequest() - Auto-strips /public directory from detected paths - Stores original base path in request attribute for URL generation - 19 unit tests with 100% code coverage - Fully backwards compatible - optional middleware Closes common 404 issues when deploying in subdirectories
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds a new
BasePathMiddlewarethat automatically strips the base path from request URIs, allowing Slim applications to run in subdirectories without modifying route definitions.🎯 Problem Statement
Typical Scenario:
Current workarounds are inelegant:
.htaccesshacks that don't work on all servers$app->get($basePath . '/api/users', ...)💡 Solution
BasePathMiddlewarestrips the base path before the request reaches the router:🚀 Usage
Option 1: Explicit Base Path
When you know the exact base path:
Apache configuration:
Option 2: Auto-Detection
When base path varies (dev vs production):
Option 3: Environment Variable
🎁 Additional Features
Access Base Path in Routes
The middleware stores the original base path in request attributes:
🧪 Deployment Examples
Shared Hosting (cPanel, etc.)
Docker with Subdirectory
Multi-tenant Application
🔄 Backwards Compatibility
🧪 Testing
Unit Tests
Integration Testing
🎯 Goal of This PR
The goal of this PR is to:
🙏 Thank You
Hope this middleware helps the Slim community simplify their deployments! 🚀