I am using Phaalcon 3.4.5, with PHP 7.3.14.
I needed to display the full-URI of a page. I have set the following when registering the service.
$url = new Phalcon\Mvc\Url(); $url->setBaseUri('/');
When I do
$this->url->get() in a controller, it only returns "/".
So I did this:
$url->setBaseUri('http' . (empty($_SERVER['HTTPS']) ? '':'s') . '://' . $_SERVER['SERVER_NAME'] . '/');
$this->url->get() again, "https://foo.localhost/" will be returned.
After changing the parameters and trying it, the result is as this:
$tmp = $this->url->get(); // "https://foo.localhost/" $tmp = $this->url->get(null, null, false); // null $tmp = $this->url->get('/', null, false); // "/" $tmp = $this->url->get(null,null,true); // "https://foo.localhost/"
From this result, I think that the default value of the third argument is true. (No description found in API manual, and the second result is not convincing to me... I think / should return.)
In most cases, what you want to get with get() is only "/ path/ to/foo/" and they don't need the hostname. However, if you set the host name etc. in baseUri in case you need full-URI, you need to set false in the local flag. (Correctly would be a "non-local" or "global" flag instead of "local")
It's been long, but what I want to know is how to display a full-URI.
Either set full-URI in baseUri and call get() with false as the third argument when displaying all that do not require full-URI, or set the base-Uri by omitting the host name etc. and generate using $_SERVER each time ?
Also, is there an easy way to generate a full-URI?
I also thought about setting up as follows and using get() and getStatic() as needed, but I'm a little uncertain and I'm not sure it's a good idea.
$url = new Mvc\Url(); $url->setBaseUri('/'); $url->setStaticBaseUri('http' . (empty($_SERVER['HTTPS']) ? '':'s') . '://' . $_SERVER['SERVER_NAME'] . $url->getBaseUri());