I've already looked at the proposed solutions on stackoverflow and I didn't find anything that solved my problem:
- Nginx + phpFPM: PATH_INFO always empty
- PATH_INFO in $_SERVER always empty - NGINX + FPM 7.3 + Ubuntu 18.04
- Nginx config rewrite with PATH_INFO
- nginx path_info in alias environemnt
- Empty value to PATH_INFO in nginx returns junk value
- Nginx: Hide .php extension but take PATH_INFO
- Debian 9 + PHP7.0-FPM + NGINX 1.10.3-1 path_info issue
- Post array always empty on CodeIgniter application running on nginx
The problem is that the PATH_INFO is still empty
The request it's ok like this: route-php.local/show
$_SERVER['REQUEST_URI']
/home/niko/web/www/route-php.local/public/index.php:46:
array (size=42)
....
'PATH_INFO' => string '' (length=0)
'QUERY_STRING' => string '' (length=0)
....
/home/niko/web/www/route-php.local/public/index.php:51:
array (size=1)
'_route' => string 'show' (length=4)
but it's not good with this request: route-php.local/show?p=test
/home/niko/web/www/route-php.local/public/index.php:46:
array (size=42)
....
'PATH_INFO' => string '' (length=0)
'QUERY_STRING' => string 'p=test' (length=6)
....
The path is not recognized because it is empty...? Why ?
$_SERVER['PATH_INFO']
with: route-php.local/show
/home/niko/web/www/route-php.local/public/index.php:46:
array (size=42)
....
'PATH_INFO' => string '' (length=0)
'QUERY_STRING' => string '' (length=0)
....
/home/niko/web/www/route-php.local/public/index.php:51:
array (size=1)
'_route' => string 'index' (length=4)
Here's my setup :
PHP-7.4 with composer and I installed symfony/routing
https://packagist.org/packages/symfony/routing
/etc/php/7.3/fpm/php.ini
/etc/php/7.3/cli/php.ini
/etc/php/7.3/apache2/php.ini
cgi.fix_pathinfo=1 # And I tried cgi.fix_pathinfo=0
/etc/nginx/sites-available/route-php.local
location / {
try_files $uri /index.php;
}
location ~ [^/]\.php(/|$) {
root /var/www/localhost;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
include snippets/fastcgi-php.conf;
}
snippets
/etc/nginx/snippets/fastcgi-php.conf
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
try_files $fastcgi_script_name =404;
set $path_info $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_INFO $path_info; # or fastcgi_param PATH_INFO $fastcgi_path_info
fastcgi_index index.php;
include fastcgi.conf;
I tried several solutions but nothing works ....
example config Symfony : https://symfony.com/doc/current/setup/web_server_configuration.html#nginx
My code PHP:
## index.php ##
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
require __DIR__ . '/vendor/autoload.php';
$listeRoute = new Route("/");
$createRoute = new Route("/create");
$showRoute = new Route("/show");
// COLLECTION ROUTES
$collection = new RouteCollection();
$collection->add("list", $listeRoute);
$collection->add("create", $createRoute);
$collection->add("show", $showRoute);
//MATCHER
$matcher = new UrlMatcher($collection, new RequestContext());
var_dump($_SERVER);
$pathInfo = $_SERVER['PATH_INFO'] ?? '/';
$resultat = $matcher->match($pathInfo);
var_dump($resultat);
die();
another Example:
php -
|-index.php
|-text.php
#index.php
var_dump($_SERVER['PATH_INFO']);
echo "index.php";
#test.php
var_dump($_SERVER['PATH_INFO']);
echo "test.php";
cgi.fix_pathinfo=1
location / {try_files $uri $uri/ /$uri.php$is_args$args;}
location ~ \.php {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php7.3-fpm.sock
}
#localhost/index
string(0) ""
index.php
#localhost/test
string(0) ""
test.php
Why it behaves like this ?
I would like this behavior
<?php
echo "PATH INFO => ";
var_dump($_SERVER['PATH_INFO']);
echo "<br>";
echo "REQUEST_URI => ";
var_dump($_SERVER['REQUEST_URI']);
// redefine the variable PATH_INFO
$_SERVER['PATH_INFO'] = explode('?', $_SERVER['REQUEST_URI']);
echo "<br>";
echo "NEW - PATH INFO => ";
var_dump($_SERVER['PATH_INFO'][0]);
echo "<br>";
echo "index.php";
# https://localhost/index?test=test
PATH INFO => string(0) ""
REQUEST_URI => string(16) "/index?test=test"
NEW - PATH INFO => string(6) "/index"
index.php