Yes, odan. Let me clarify little a bit. I mean it works only after your suggestion, but it does not work according the Slim 4 docs. I tested your solution just after your first reply for this topic.
I think your suggestion should be taken into account in the Slim 4 docs. In my opinion, Slim 4, as microframework, should work as expected and noticed in docs, and with minimal configuration.
The last days I played with Slim 4 and tried to find a more āelegantā solution for apache users running a Slim 4 app in a documentRoot public/ or a sub-directory of documentRoot.
Then I discovered $app->setBasePath($basePath). You can pass a fix value or let a function (library) detect the real basePath for you.
Example:
$app = AppFactory::create();
// 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);
// It can be read http://www.slimframework.com/docs/v4/deployment/deployment.html
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (^[^/]*$) public/$1 [L]
</IfModule>
// or @odan suggestion
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
yourproject/public/.htaccess
// It can be read http://www.slimframework.com/docs/v4/start/web-servers.html
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
yourproject/public/index.php
// It can be read http://www.slimframework.com/docs/v4/start/installation.html
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
require __DIR__ . '/../vendor/autoload.php';
// start of the @odan suggestion
$path = parse_url($_SERVER['REQUEST_URI'])['path'];
$scriptName = dirname(dirname($_SERVER['SCRIPT_NAME']));
$len = strlen($scriptName);
if ($len > 0 && $scriptName !== '/') {
$path = substr($path, $len);
}
$_SERVER['REQUEST_URI'] = $path ?: '';
// end of the @odan suggestion
$app = AppFactory::create();
$app->get('/', function (Request $request, Response $response, $args) {
$response->getBody()->write("Hello world!");
return $response;
});
$app->run();