Symfony\Component\HttpFoundation\Request::getPort PHP Method

getPort() public method

This method can read the client port from the "X-Forwarded-Port" header when trusted proxies were set via "setTrustedProxies()". The "X-Forwarded-Port" header must contain the client port. If your reverse proxy uses a different header name than "X-Forwarded-Port", configure it via "setTrustedHeaderName()" with the "client-port" key.
public getPort ( ) : string
return string
    public function getPort()
    {
        if ($this->isFromTrustedProxy()) {
            if (self::$trustedHeaders[self::HEADER_CLIENT_PORT] && $port = $this->headers->get(self::$trustedHeaders[self::HEADER_CLIENT_PORT])) {
                return $port;
            }

            if (self::$trustedHeaders[self::HEADER_CLIENT_PROTO] && 'https' === $this->headers->get(self::$trustedHeaders[self::HEADER_CLIENT_PROTO], 'http')) {
                return 443;
            }
        }

        if ($host = $this->headers->get('HOST')) {
            if ($host[0] === '[') {
                $pos = strpos($host, ':', strrpos($host, ']'));
            } else {
                $pos = strrpos($host, ':');
            }

            if (false !== $pos) {
                return (int) substr($host, $pos + 1);
            }

            return 'https' === $this->getScheme() ? 443 : 80;
        }

        return $this->server->get('SERVER_PORT');
    }

Usage Example

Example #1
0
 public function __construct($routes, Request $request, EventDispatcher $dispatcher, ControllerResolver $resolver)
 {
     $this->deflRes = new NodeResponse();
     $this->context = new RequestContext($request->getBaseUrl(), $request->getMethod(), $request->getHost(), $request->getScheme(), $request->getPort(), $request->getPort());
     $this->matcher = new UrlMatcher($routes, $this->context);
     parent::__construct($dispatcher, $resolver);
 }
All Usage Examples Of Symfony\Component\HttpFoundation\Request::getPort