Skip to content

Instantly share code, notes, and snippets.

@tristanlins
Created October 27, 2014 09:25
Show Gist options
  • Save tristanlins/2e28acfb8b25b701a760 to your computer and use it in GitHub Desktop.
Save tristanlins/2e28acfb8b25b701a760 to your computer and use it in GitHub Desktop.
Replace contao Environment class with symfony request class.

Replacement possible

Environment::scriptName()
  => string(33) "/environment-vs-request/index.php"
Request::getBaseUrl()    
  => string(33) "/environment-vs-request/index.php"
Environment::phpSelf()
  => string(33) "/environment-vs-request/index.php"
Request::getBaseUrl() 
  => string(33) "/environment-vs-request/index.php"
Environment::documentRoot()                            
  => string(29) "/home/tristan/public_html/web"
Container::getParameter('kernel.root_dir') . '/../web')
  => string(55) "/home/tristan/public_html/environment-vs-request/../web"
Environment::requestUri()
  => string(48) "/environment-vs-request/some/path?with=arguments"
Request::getRequestUri() 
  => string(48) "/environment-vs-request/some/path?with=arguments"
Environment::httpAcceptLanguage()
  => array(4) {
       [0] =>
       string(5) "de-DE"
       [1] =>
       string(2) "de"
       [2] =>
       string(5) "en-US"
       [3] =>
       string(2) "en"
     }
Request::getLanguages()          
  => array(4) {
       [0] =>
       string(5) "de_DE"
       [1] =>
       string(2) "de"
       [2] =>
       string(5) "en_US"
       [3] =>
       string(2) "en"
     }
Environment::httpAcceptEncoding()
  => array(3) {
       [0] =>
       string(4) "gzip"
       [1] =>
       string(7) "deflate"
       [2] =>
       string(4) "sdch"
     }
Request::getEncodings()          
  => array(3) {
       [0] =>
       string(4) "gzip"
       [1] =>
       string(7) "deflate"
       [2] =>
       string(4) "sdch"
     }
Environment::httpUserAgent()        
  => string(105) "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36"
Request::$headers->get('user-agent')
  => string(105) "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36"
Environment::httpHost()
  => string(9) "localhost"
Request::getHttpHost() 
  => string(9) "localhost"
Environment::ssl() 
  => bool(false)
Request::isSecure()
  => bool(false)
Environment::url()                               
  => string(16) "http://localhost"
Request::getScheme() . '://' . Request::getHost()
  => string(16) "http://localhost"
Environment::uri()
  => string(64) "http://localhost/environment-vs-request/some/path?with=arguments"
Request::getUri() 
  => string(64) "http://localhost/environment-vs-request/some/path?with=arguments"
Environment::ip()     
  => string(9) "127.0.0.1"
Request::getClientIp()
  => string(9) "127.0.0.1"
Environment::server()               
  => string(9) "127.0.0.1"
Request::$server->get('SERVER_ADDR')
  => string(9) "127.0.0.1"
Environment::host()   
  => string(9) "localhost"
Request::getHttpHost()
  => string(9) "localhost"
Environment::isAjaxRequest()
  => bool(false)
Request::isXmlHttpRequest() 
  => bool(false)

Replacement may not return same result

The documentation of Request::getHost() says, that it use the X-Forward-Host header, if it's trusted. But I cannot get it work :-\

Environment::httpXForwardedHost()
  => string(17) "local.localdomain"
Request::getHost()               
  => string(9) "localhost"

Replacement imo not necessary

These methods are not necessary imo, urls should be buildet with the url builder / router.

Environment::path()   
  => string(23) "/environment-vs-request"
Request::getBasePath()
  => string(23) "/environment-vs-request"
Environment::base()                                                             
  => string(40) "http://localhost/environment-vs-request/"
Request::getScheme() . '://' . Request::getHost() . Request::getBasePath() . '/'
  => string(40) "http://localhost/environment-vs-request/"
Environment::scriptFilename()
  => string(58) "/home/tristan/public_html/environment-vs-request/index.php"
Environment::script()
  => string(9) "index.php"
Environment::request()
  => string(24) "some/path?with=arguments"
Environment::indexFreeRequest()
  => string(24) "some/path?with=arguments"

Replacement impossible (alternative required)

Environment::agent()
  => string(3) "..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment