HttpFoundation (Symfony Component)


Il componente HttpFoundation definisce un livello orientato agli oggetti per la specifica HTTP.

In PHP la richiesta è rappresentata da alcune variabili globali ( $_GET, $_POST, $_FILES, $_COOKIE, $_SESSION, ...) e la risposta è generata da alcune funzioni ( echo, header(), setcookie(), ...).

Il componente Symfony HttpFoundation sostituisce queste variabili e funzioni globali PHP predefinite con un livello orientato agli oggetti.


 documentation  source

Installazione

$composer require symfony/http-foundation

Test 1

Verifica metodi
  • ->getSchemeAndHttpHost()
  • ->getBaseUrl()

code:


result:


$request->isSecure():
^ true

$request->getScheme():
^ "https"

$request->getHost():
^ "www.symfony.andreamodolo.it"

$request->getHttpHost():
^ "www.symfony.andreamodolo.it"

$request->getSchemeAndHttpHost():
^ "https://www.symfony.andreamodolo.it"

$request->getUri():
^ "https://www.symfony.andreamodolo.it/src/httpfoundation/index.php/?foo=bar"

$request->getRequestUri():
^ "/src/httpfoundation/index.php?foo=bar"

$request->getBaseUrl():
^ "/src/httpfoundation/index.php"

$request->getClientIp():
^ "216.73.217.104"

$request->getScriptName():
^ "/src/httpfoundation/index.php"

$request->getPathInfo():
^ "/"

$request->getBasePath():
^ "/src/httpfoundation"

$request->getPort():
^ 443

$request->getUser():
^ null

$request->getUserInfo():
^ null

$request->getPassword():
^ null

$request->getQueryString():
^ "foo=bar"

$request->getMethod():
^ "GET"

$request->getRealMethod():
^ "GET"

Request::getMimeTypes('html'):
^ array:2 [
  0 => "text/html"
  1 => "application/xhtml+xml"
]

$request->getMimeType('html'):
^ "text/html"

$request->getFormat(''):
^ null

$request->getFormat($request->getMimeType('html')):
^ "html"

$request->getRequestFormat('html'):
^ "html"

$request->getContentType():
^ null

$request->getDefaultLocale():
^ "en"

$request->getLocale():
^ "en"

$request->getProtocolVersion():
^ "HTTP/1.1"

$request->getContent(false):
^ ""

$request->getETags():
^ []

$request->isNoCache():
^ false

$request->getPreferredLanguage(null):
^ null

$request->getLanguages():
^ []

$request->getCharsets():
^ []

$request->getEncodings():
^ array:4 [
  0 => "gzip"
  1 => "br"
  2 => "zstd"
  3 => "deflate"
]

$request->getAcceptableContentTypes():
^ array:1 [
  0 => "*/*"
]

$request->isXmlHttpRequest():
^ false

$request->preferSafeContent():
^ false

$request->isFromTrustedProxy():
^ false

$request->getSession():
Session has not been set.

Test 2

Verifica metodo ->query->get()
Usa la query string '?foo=bar'

Simula il test qui: https://www.symfony.andreamodolo.it/src/httpfoundation/index.php? foo=bar

code:


result:


$request->query->get('foo'):
^ "bar"

$request->query->get('bar'):
^ null

$request->query->get('bar','defaultValue'):
^ "defaultValue"

$request->query->get('bar'):
^ null

Test 3

Verifica metodo ->query->all()
Usa la query string '? foo=bar &baz=john &name=Jack &foo2[bar]=baz'

Simula il test qui: https://www.symfony.andreamodolo.it/src/httpfoundation/index.php? foo=bar &baz=john &name=Jack &foo2[bar]=baz

code:


result:


$request->query->all():
^ array:1 [
  "foo" => "bar"
]

$request->query->all()['foo']:
^ "bar"

$request->query->all()['foo2']:
^ null

$request->query->all()['foo2']['bar']:
^ null

$request->query->count():
^ 1

Test 4

Simulating a Request
Invece di creare una richiesta basata sui globali PHP, si può anche simulare una richiesta

code:


result:


Request::create( ... ):
^ Symfony\Component\HttpFoundation\Request {#10
  +attributes: Symfony\Component\HttpFoundation\ParameterBag {#12
    #parameters: []
  }
  +request: Symfony\Component\HttpFoundation\InputBag {#13
    #parameters: []
  }
  +query: Symfony\Component\HttpFoundation\InputBag {#16
    #parameters: array:1 [
      "name" => "Fabien"
    ]
  }
  +server: Symfony\Component\HttpFoundation\ServerBag {#17
    #parameters: array:17 [
      "SERVER_NAME" => "localhost"
      "SERVER_PORT" => 80
      "HTTP_HOST" => "localhost"
      "HTTP_USER_AGENT" => "Symfony"
      "HTTP_ACCEPT" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
      "HTTP_ACCEPT_LANGUAGE" => "en-us,en;q=0.5"
      "HTTP_ACCEPT_CHARSET" => "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
      "REMOTE_ADDR" => "127.0.0.1"
      "SCRIPT_NAME" => ""
      "SCRIPT_FILENAME" => ""
      "SERVER_PROTOCOL" => "HTTP/1.1"
      "REQUEST_TIME" => 1791545172
      "REQUEST_TIME_FLOAT" => 1791545172.1878
      "PATH_INFO" => ""
      "REQUEST_METHOD" => "GET"
      "REQUEST_URI" => "/hello-world?name=Fabien"
      "QUERY_STRING" => "name=Fabien"
    ]
  }
  +files: Symfony\Component\HttpFoundation\FileBag {#14
    #parameters: []
  }
  +cookies: Symfony\Component\HttpFoundation\InputBag {#11
    #parameters: []
  }
  +headers: Symfony\Component\HttpFoundation\HeaderBag {#15
    #headers: array:5 [
      "host" => array:1 [
        0 => "localhost"
      ]
      "user-agent" => array:1 [
        0 => "Symfony"
      ]
      "accept" => array:1 [
        0 => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
      ]
      "accept-language" => array:1 [
        0 => "en-us,en;q=0.5"
      ]
      "accept-charset" => array:1 [
        0 => "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
      ]
    ]
    #cacheControl: []
  }
  #content: null
  #languages: null
  #charsets: null
  #encodings: null
  #acceptableContentTypes: null
  #pathInfo: null
  #requestUri: null
  #baseUrl: null
  #basePath: null
  #method: null
  #format: null
  #session: null
  #locale: null
  #defaultLocale: "en"
  -preferredFormat: null
  -isHostValid: true
  -isForwardedValid: true
  pathInfo: "/hello-world"
  requestUri: "/hello-world?name=Fabien"
  baseUrl: ""
  basePath: ""
  method: "GET"
  format: "html"
}