laravel octane 中的未定义数组键 "REMOTE_ADDR"

Undefined array key "REMOTE_ADDR" in laravel octane

我正在使用 laravel 8。在升级到 octane 之前,我在 $_SERVER['REMOTE_ADDR'] 中获取 IP 地址。改成octane后$_SERVER中就没有这样的索引了。

我在注册控制器的验证器中使用这个变量,而新用户正在注册到应用程序。

 protected function validator(array $data)
      {
 dd($_SERVER['REMOTE_ADDR'])
}

当我转储 $_SERVER 时,我得到了这个数组(我隐藏了应用程序的详细信息。)

  "LARAVEL_OCTANE" =>"1"
  "APP_BASE_PATH" => "xxx"
  "JOURNAL_STREAM" => "xxx"
  "PATH" =>"xxx"
  "INVOCATION_ID" => "xxx"
  "LANG" => "xxx"
  "SUPERVISOR_ENABLED" =>"xxx"
  "SUPERVISOR_SERVER_URL" => "xxx"
  "SUPERVISOR_PROCESS_NAME" =>"xxx"
  "APP_ENV" => "xxx"
  "PWD" => "xxx"
  "PHP_SELF" => "xxx"
  "SCRIPT_NAME" => "xxx"
  "SCRIPT_FILENAME" =>"xxx"
  "PATH_TRANSLATED" => "xxx"
  "DOCUMENT_ROOT" => ""
  "REQUEST_TIME_FLOAT" => "xxx"
  "REQUEST_TIME" => "xxx"
  "argv" => array:2 [▶]
  "argc" =>"xxx"
  "APP_NAME" => "xxx"
  "APP_KEY" => "xxx"
  "APP_DEBUG" => "xxx"
  "APP_LOG_LEVEL" => "xxx"
  "APP_URL" => "xxx"
  "OCTANE_SERVER" =>"xxx"
  "LOG_CHANNEL" => "xxx"
  "DB_CONNECTION" => "xxx"
  "DB_HOST" => "xxx"
  "DB_PORT" => "xxx"
  "DB_DATABASE" => "xxx"
  "DB_USERNAME" =>"xxx"
  "DB_PASSWORD" => "xxx"
  "BROADCAST_DRIVER" => "xxx"
  "CACHE_DRIVER" => "xxx"
  "SESSION_DRIVER" => "xxx"
  "SESSION_LIFETIME" => "xxx"
  "QUEUE_CONNECTION" => "xxx"
  ...............
 

我找到了这个问题的解决方案。正如@Rwd 评论的那样 更换 $_SERVER['REMOTE_ADDR'] 来自 request()->ip()

它工作正常。 (octane中PHP的全局变量在请求初始化的时候是不会初始化的,比如$_SERVER,$_POST,$_Get等。具体可以看下面的文章。https://developpaper.com/php-fpm-vs-swoole/)