-
Notifications
You must be signed in to change notification settings - Fork 123
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
getAbsoluteUrl
not including the host
#708
Comments
Yup figured out the same, but I am afraid it is going to be a bit more complicated to fix than a simple The A quick googlesearch would reveal several attempts to 'reconstruct' an url-string from scratch, but I think it is better to make the |
Well, I took a quick stab at a more proper way of building the /**
* Create url from global $_SERVER data.
* Note, this does not include validation so be carefull when using the returned result!
* @param bool checkForwarded `true` to include forwarded headers when checking/obtaining fields.
*/
static function createUrl(bool $checkForwarded = true)
{
function s($name)
{
if (isset($_SERVER[$name]))
return $_SERVER[$name];
return null;
}
$url = new Url(s('REQUEST_URI'));
// Checking if http(s) protocol is used, in that case try proper handling, otherwise hope for the best and take server protocol literally
if (stripos((s('SERVER_PROTOCOL') != null ? s('SERVER_PROTOCOL') : (s('REQUEST_SCHEME') != null ? s('REQUEST_SCHEME') : '')), 'http') !== false) {
if ((s('HTTPS') == 'on' || s('HTTPS') == 1) ||
$checkForwarded ? (s('HTTP_X_FORWARDED_PROTO') == 'https') : false
)
$url->setScheme('https');
else
$url->setScheme('http');
} else
$url->setScheme((s('SERVER_PROTOCOL') != null) ? s('SERVER_PROTOCOL') : s('REQUEST_SCHEME'));
// Setting the host from a request is messy, proper validstion or else substitution of internal/fixed variable is advised.
$host = null;
if ($checkForwarded) {
if (s('HTTP_X_FORWARDED_HOST') != null)
$host = s('HTTP_X_FORWARDED_HOST');
}
if ($host == null && s('SERVER_NAME') != null)
$host = s('SERVER_NAME');
if ($host == null && s('HTTP_HOST') != null)
$host = s('HTTP_HOST');
// This is a default/fallback value as setting the hostname reliably requires environment/engine-dependent configuration. Handy quick-fix if all else fails.
// if ($host == null)
// $host = Config::$_HOST_NAME;
// some systems and/or header-variants include the port, this would strip out the host in case it is present
if(stripos($host, ':') != false)
$host = parse_url($host, PHP_URL_HOST);
$url->setHost($host);
if (s('SERVER_PORT') != null)
$url->setPort(s('SERVER_PORT'));
// Reset query-string as soms system removes parameters when multiple are present
if (s('QUERY_STRING') != null)
$url->setQueryString(s('QUERY_STRING'));
// Following two properties may not be available with some php-engines, so don't rely on it being present with incomming requests.
if (s('PHP_AUTH_USER') != null)
$url->setPassword(s('PHP_AUTH_USER'));
if (s('PHP_AUTH_PW') != null)
$url->setUsername(s('PHP_AUTH_PW'));
return $url;
} The above can be used to generate an // either one of following for obtaining the router instance
$router = new Router();
$router = SimpleRouter::router();
$router->getRequest()->setUrl(createUrl()) This will 'reset' the One side-note with obtaining the |
Usually, I make fixes for things like this myself But looks like it's going to cause one problem or the other that's why I created the issue for the author to look into |
I use laragon for local development and running
I first noticed something like
url('home.test')->getAbsoluteUrl()
only returned the path (no scheme, no host)After trying with
request()->getUrl()->getAbsoluteUrl()
the scheme showed here, but the host was emptyThe above code produced this
"https:///_manage/test/"
On inspection, i made this change in
Pecee\Http\Request
and everything worked fine for:
But
url('home.page')->getAbsoluteUrl()
still returned only the path (without the host & scheme)If you could look into this. I can provide more information if you need
The text was updated successfully, but these errors were encountered: