How can I get the full-URI

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'] . '/');

Then $this->url->get() again, "http://foo.localhost/" will be returned. After changing the parameters and trying it, the result is as this:

$tmp = $this->url->get();   // "http://foo.localhost/"
$tmp = $this->url->get(null, null, false);  // null
$tmp = $this->url->get('/', null, false);  // "/"
$tmp = $this->url->get(null,null,true);  // "http://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());

Hey @s-ohnishi, Did you get the solution? Please comment the solution here.



1.9k

Thank you for your reply. Since nobody could reply to me, I was updating to Phalcon4, so I tried an additional test.

As you can see, there is no change in the value that can be acquired with the same argument as the previous time.

$url->setBaseUri('http' . (empty($_SERVER['HTTPS']) ? '':'s') . '://' . $_SERVER['SERVER_NAME'] . '/');
$tmp = $this->url->get();   // "http://foo.localhost/"
$tmp = $this->url->get(null, null, false);  // "http://foo.localhost/"
$tmp = $this->url->get('/', null, false);  // "http://foo.localhost/"
$tmp = $this->url->get(null, null, true);  // "http://foo.localhost/"

In Phalcon3, the result differs depending on the argument, but in Phalcon4, the result did not change with this argument.

To be honest, I no longer know how to specify these parameters.

By the way, there was no suggestion from anyone about how to get the original purpose full-URI, so I decided to distinguish it with "static". As I showed when I asked, I have setBaseUri(), setStaticBaseUri() set and use getStatic() when I want a full-URI, otherwise I use get().

I don't think this method makes sense, but for the time being, I intend to use it.

Hey @s-ohnishi, Did you get the solution? Please comment the solution here.