Skip to content
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

UrlImmutable should implement static get() method #201

Closed
janbarasek opened this issue Jun 28, 2021 · 5 comments
Closed

UrlImmutable should implement static get() method #201

janbarasek opened this issue Jun 28, 2021 · 5 comments

Comments

@janbarasek
Copy link
Contributor

Class UrlImmutable should implement basic static method for get current URL.

Sample implementation:

public static function get(): static
{
	if (!isset($_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'])) {
		throw new \RuntimeException('URL detection is not available in CLI mode.');
	}
	$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http')
		. '://' . strtolower($_SERVER['HTTP_HOST']) . $_SERVER['REQUEST_URI'];
	return new static($url);
}

I know Singleton can be evil sometimes (https://phpfashion.com/singleton-sofie-s). On the other hand, having the ability to quickly access the actual URL is completely missing from the framework and has to be worked around in various ways.

Thanks.

@dg
Copy link
Member

dg commented Jun 28, 2021

If you know it's wrong, why are you sending PR?

@dg dg closed this as completed Jun 28, 2021
@janbarasek
Copy link
Contributor Author

Because there is no simple native way to get the actual request URL.

@mabar
Copy link
Contributor

mabar commented Jun 28, 2021

Nette\Http\IRequest->getUrl()?

@janbarasek
Copy link
Contributor Author

@mabar This notation is too complicated for beginners. You have to somehow get the request through the DIC, from which you only get the URL. If I want to figure out how to get the URL, I would expect to "somehow" create an instance of the URL and simply read the data.

@dg
Copy link
Member

dg commented Jun 28, 2021

The fact that there is no simple way to get the current URL is no reason to implement it badly.

Btw I think that this way is pretty simple: (new RequestFactory)->fromGlobals()->getUrl()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants