Skip to content

Request::createFromGlobals() doesn't work #59533

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

Closed
starline opened this issue Jan 17, 2025 · 15 comments
Closed

Request::createFromGlobals() doesn't work #59533

starline opened this issue Jan 17, 2025 · 15 comments

Comments

@starline
Copy link

Symfony version(s) affected

6.4.17

Description

Image

php version: 8.1.7

$_GET is empty
$_POST is empty

How to reproduce

i have HTTP 500 Internal Server Error when try to get Request by Request::createFromGlobals()

Possible Solution

add additional type (array|null) of variables $query and $query and $cookies

like that:
private static function createRequestFromFactory(array|null $query = [], array|null $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null): stat

Additional Context

No response

@starline
Copy link
Author

this is error
Image

@starline
Copy link
Author

i've founded more useful solution here
https://stackoverflow.com/questions/45320353/how-to-use-default-value-when-null-is-given-for-a-nullable-function-parameter

private static function createRequestFromFactory(?array $query = null, ....l) {
$query = $query ?? [];
...
}

@xabbuh
Copy link
Member

xabbuh commented Jan 17, 2025

Please provide more information on how to reproduce your issue. What is the setup you have that leads to the PHP superglobals not being set?

@starline
Copy link
Author

starline commented Jan 17, 2025

i am making request from independent script. It's not service or controller

<?php
namespace GoodGin\Extensions\TestScripts;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use GoodGin\Extensions\BaseExtension;

final class TestScripts extends BaseExtension
{
    public function index()
    { 
         $request = Request::createFromGlobals();

         ... other code
    }
}

This happened because empty $_POST return NULL. and function createRequestFromFactory can receive only Array type

@xabbuh
Copy link
Member

xabbuh commented Jan 17, 2025

In this case you shouldn’t use the createFromGlobals() method if they are not defined.

@starline
Copy link
Author

I am following the instructions.
https://symfony.com/doc/current/components/http_foundation.html

use Symfony\Component\HttpFoundation\Request;
$request = Request::createFromGlobals();

what i do wrang?

@xabbuh
Copy link
Member

xabbuh commented Jan 17, 2025

The article assumes that you are running the script in an HTTP context and not on the command-line.

@starline
Copy link
Author

starline commented Jan 18, 2025

I made it in an HTTP context.
right now I've checked this in main controller and have same error.

Symfony\Component\HttpFoundation\Request::createRequestFromFactory(): Argument #1 ($query) must be of type array, null given, called in /var/www/dev.grizlicnc.com.ua/vendor/symfony/http-foundation/Request.php on line 305

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;

class Main extends AbstractController
{
    #[Route('/', name: 'Main')]
    public function main($_route): Response
    {
        var_dump(Request::createFromGlobals());
}
}

Please check createRequestFromFactory arguments, and you will get it

@xabbuh
Copy link
Member

xabbuh commented Jan 18, 2025

Please show the output of the following lines:

print_r($_SERVER);
print_r($_GET);
print_r($_POST);

@starline
Copy link
Author

starline commented Jan 18, 2025

Array
(
[USER] => www-data
[HOME] => /var/www
[HTTP_COOKIE] => PHPSESSID=*;
[HTTP_ACCEPT_LANGUAGE] => en-US,en;q=0.9,ru-UA;q=0.8,ru;q=0.7,ro;q=0.6
[HTTP_ACCEPT_ENCODING] => gzip, deflate, br, zstd
[HTTP_REFERER] => https://grizlicnc.com.ua/agmin/extension/TestScripts
[HTTP_SEC_FETCH_DEST] => document
[HTTP_SEC_FETCH_USER] => ?1
[HTTP_SEC_FETCH_MODE] => navigate
[HTTP_SEC_FETCH_SITE] => same-origin
[HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,
/
;q=0.8,application/signed-exchange;v=b3;q=0.7
[HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
[HTTP_UPGRADE_INSECURE_REQUESTS] => 1
[HTTP_CONTENT_TYPE] => application/x-www-form-urlencoded
[HTTP_ORIGIN] => https://grizlicnc.com.ua
[HTTP_SEC_CH_UA_PLATFORM] => "Windows"
[HTTP_SEC_CH_UA_MOBILE] => ?0
[HTTP_SEC_CH_UA] => "Not A(Brand";v="8", "Chromium";v="132", "Google Chrome";v="132"
[HTTP_CACHE_CONTROL] => max-age=0
[HTTP_CONTENT_LENGTH] => 63
[HTTP_CONNECTION] => keep-alive
[HTTP_HOST] => grizlicnc.com.ua
[REDIRECT_STATUS] => 200
[HTTPS] => on
[SERVER_NAME] => grizlicnc.com.ua
[SERVER_PORT] => 443
[SERVER_ADDR] => 185.67.2.159
[REMOTE_PORT] => 51748
[REMOTE_ADDR] => 174.51.199.221
[SERVER_SOFTWARE] => nginx/1.10.3
[GATEWAY_INTERFACE] => CGI/1.1
[SERVER_PROTOCOL] => HTTP/1.1
[DOCUMENT_ROOT] => /var/www/grizlicnc.com.ua/public
[DOCUMENT_URI] => /index.php
[REQUEST_URI] => /agmin/extension/TestScripts
[PATH_INFO] =>
[SCRIPT_NAME] => /index.php
[SCRIPT_FILENAME] => /var/www/grizlicnc.com.ua/public/index.php
[CONTENT_LENGTH] => 63
[CONTENT_TYPE] => application/x-www-form-urlencoded
[REQUEST_METHOD] => POST
[QUERY_STRING] =>
[FCGI_ROLE] => RESPONDER
[PHP_SELF] => /index.php
[REQUEST_TIME_FLOAT] => 173722370.8739
[REQUEST_TIME] => 1737224370
[APP_ENV] => dev
[APP_SECRET] => ***
[SYMFONY_DOTENV_VARS] => APP_ENV,APP_SECRET
[APP_DEBUG] => 1
[SHELL_VERBOSITY] => 3
[DOCTRINE_DEPRECATIONS] => trigger
)
Array
(
[csrf] => ***
[action] => script
[do_script] => 1
)

@starline
Copy link
Author

starline commented Jan 18, 2025

additional i'll show var_dump($_GET)

/var/www/grizlicnc.com.ua/goodgin/Extensions/TestScripts/TestScripts.php:43:null

null - because $_GET is empty

@xabbuh
Copy link
Member

xabbuh commented Jan 18, 2025

Could it be that the variables_order setting of your PHP Installation does not contain G?

@xabbuh
Copy link
Member

xabbuh commented Jan 20, 2025

I just checked but even if I set variables_order to S the $_GET superglobal is still populated as an empty array. So I am afraid you will have to debug your setup to find out why it's null in your case or provide an example repository with a Docker setup that allows to reproduce it.

@starline
Copy link
Author

hmm. it is really strange confuse. but my several different servers return NULL for empty $_POST $_GET.

They use
Debian stretch

php8.1-fpm with nginx

PHP 8.1.7 (cli) (built: Jun 25 2022 08:17:26) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.7, Copyright (c) Zend Technologies
with Zend OPcache v8.1.7, Copyright (c), by Zend Technologies
with Xdebug v3.1.5, Copyright (c) 2002-2022, by Derick Rethans

i changed in php.ini
register_argc_argv
auto_globals_jit

but it didn't help

Then... i'v checked this response in index.php and get
/var/www/dev.grizlicnc.com.ua/public/index.php:3:
array (size=0)
empty

it's really interesting

and i found something like that in one helper package:

$_POST = $this->stripslashes_recursive($_POST);

  private function stripslashes_recursive($var)
  {
      $res = null;  # this is why it returned NULL
      if (is_array($var)) {
          foreach ($var as $k => $v) {
              $res[stripcslashes($k)] = $this->stripslashes_recursive($v);
          }
      } else {
          $res = stripcslashes($var);
      }

      return $res;
  }

so, the question is done

thank you for you time. really i am appreciate you

@xabbuh
Copy link
Member

xabbuh commented Jan 21, 2025

Great you found the cause. 👍 I am closing the issue then.

@xabbuh xabbuh closed this as completed Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants