New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
4.x - Base path handling #2512
Comments
This should be moved to Slim-Psr7 repo along with #1964 |
This essentially means we should support running Slim in a subdirectory. i.e. Slim should work for a URL like www.example.com/myapp/hello where the route in Slim is I would like this to be PSR-7 implementation independent. |
Slim already works when running from a subdirectory. I do it all the time. Just need to set the
|
@tuupola wouldn't it be better to make it server environment independent somehow? We could maybe add a setting to |
@l0gicgate I don't have any strong feelings about that. However one cannot get rid of Lets assume the document root is
$app = new \Slim\App;
$app->get("/hello", function ($request, $response, $args) {
print "Hello!";
});
$app->run();
Without
With
Since |
I don't believe nginx has this problem since when you setup fastcgi you have to set the base path like so:
@akrabat better documentation perhaps? This cannot be fixed at the Slim level from my understanding. |
I'm more than happy to have a definitive documentation page on how to run Slim in a sub-directory. |
For Apache all you need to do is to add Example of an
The
Since it is bad habit to have
With above folder structure
Now source code is outside document root but Slim app is still accessible at |
@tuupola Maybe I think Slim 4 should handle this issue, like Symfony and other Frameworks can handle it. In Slim 3 I do a little "hack" by...
I hope that in Slim 4 this "hack" is no longer needed. :-) |
@odan can you point me where the logic is from Symfony to circumvent this issue? Because I'm looking at Zend Expressive and they explicitly document that a server side configuration is required: I'd like to fix this for |
It looks like I confused it with a feature in CakePHP. Update: Here is my working solution using The basic directory structure:
For Apache we have to "redirect" the web traffic to the front controller in Create a file:
We also need a rule to "redirect" the sub-directories to the front-controller in Create a
Set the base path to run the app in a subdirectory. This path is used in urlFor(). Example: <?php
use Slim\Factory\AppFactory;
use Selective\BasePath\BasePathDetector;
$app = AppFactory::create();
// composer require selective/basepath
// Set the base path to run the app in a subdirectory.
// This path is used in urlFor().
$basePath = (new BasePathDetector($_SERVER))->getBasePath();
$app->setBasePath($basePath);
// Add middleware
// ...
$app->run(); |
I just installed Slim 4.0.0-alpha, configured an alias called
In the same subdirectory I then added a very simple use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
include_once(__DIR__ . '/../vendor/autoload.php');
define('ROUTE_NAME_ANSWER', 'answer');
$app = AppFactory::create();
$app->setBasePath('/slim4');
$app->get('/', function (Request $request, Response $response, $args) use ($app) {
$routeParser = $app->getRouteCollector()->getRouteParser();
$response->getBody()->write('<a href="' . $routeParser->urlFor(ROUTE_NAME_ANSWER) . '">What is the answer?</a>');
return $response;
});
$app->get('/answer', function (Request $request, Response $response, $args) {
$response->getBody()->write('42');
return $response;
})->setName(ROUTE_NAME_ANSWER);
$app->run(); Requesting Of course if would be great if the base path could either be detected automatically, or if it could be set via settings. One possibility would be to use Apache environment variables (if allowed by hosting provider):
$app->setBasePath($_SERVER['SLIM_BASE_PATH']); |
@adriansuter that's a good solution. I suppose we should add that to the docs? Can we consider this issue resolved as well? |
@l0gicgate Maybe in the docs we should use I haven't checked all possible use cases of the base path. Other than that, in my opinion the concept is pretty consistent and without confusion, as requested by @akrabat |
I just started looking at v4 alpha and directly saw this issue just after copying the hello world code. I wondered why Slim 3 works out the box and found some base-path-detection code here: I would guess this was removed because of the PSR7 changes. |
@moritz-h we could possibly include some type of middleware which appends the user defined base path to the request's uri's path. We could do something like https://github.com/Lansoweb/basepath does. |
@l0gicgate that's a good idea. |
@l0gicgate I think you mean something different. You talk about handling the user defined base path. What I meant is, that I do not want to define a base bath and Slim should auto-detect it (like Slim v3 does, when I run it in a subdirectory). Currently I cloud either define What I was suggesting above is that perhaps in AppFactory the auto-detection is done, so there there is no need for calling |
@moritz-h feel free to raise a PR with base path detection logic in |
@l0gicgate I read #2107 only after my last post here. Before I only read Slim 3 sources and thought it is a matter of copy and paste and adjust this part of code. Now I see what the problem is with this. |
I implemented a <?php
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use Psr\Http\Message\ResponseInterface;
class BasePathMiddleware
{
private $basePath;
public function __construct(string $basePath)
{
$this->basePath = $basePath;
}
public function __invoke(Request $request, RequestHandler $handler): ResponseInterface
{
$uri = $request->getUri();
$path = $uri->getPath();
$path = '/'.ltrim($path, $this->basePath);
$request = $request->withUri($uri->withPath($path));
return $handler->handle($request);
}
} Use it like: $app->addRoutingMiddleware();
$basePathMiddleware = new BasePathMiddleware('/api');
$app->add($basePathMiddleware); |
I left a note about this in #2770 (comment) because it tripped me up during migration from v3 to v4. I didn't see this open issue at the time. |
any idea, why PSR7 implementations are not using: $_SERVER['PATH_INFO'] ? |
@dominikzogg |
IMO it should me mentionned on this page: http://www.slimframework.com/docs/v4/start/web-servers.html |
|
@piotr-cz that's an interesting bug. Let's open an issue for that. |
I'm closing this as resolved To set base path use: |
thank you @adriansuter , work for me in URL : http://localhost/slim4/public/ and structure folder : Click here for view
my htaccess:
my index.php ( the same of http://www.slimframework.com/)
|
We need to have base path handling working consistently without confusion in Slim 4.
See also: #1964
The text was updated successfully, but these errors were encountered: