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.



3.4k

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.



3.4k

After playing around with it, I came to the conclusion that the previous proposal was not acceptable. This is because using staticBaseUri in this way is inconvenient when using tag helpers to include style sheets or JavaScript.

I want to specify baseUri and then the following when including these with volt:

{{ stylesheet_link( 'css/foo.css') }}

I think that this is necessary in order to give the final installation directory flexibility.

Looking at the document I see the following

public static function stylesheetLink( mixed $parameters = null, bool $local = bool ): string;

(maybe bool $local = bool is a mistake of bool $local = true)

When I looked into the source, the flag $local seemed to mean whether to use staticBaseUri or not.

The name "local" hindered my understanding, but if local is specified (=true), it is a relative path from the project public directory, and if it is not specified (=false), it seems to be a relative path from the current directory (= directory in the URL field of the browser). Normally, you will put the css/ directory in the project public directory, so you should specify true (this is the default value).

If staticBaseUri is not set, it seems to be the same as baseUri, so it works well, but if you set something to staticBaseUri, an unexpected path will be output, so I think that staticBaseUri should not be set. I will.

$di->setShared('url', function () {
    $config = $this->getConfig();

    $url = new Phalcon\Url();
    $url->setBaseUri('/bar/');
    $url->setStaticBaseUri('http://example.com/bar/');

    return $url;
});

Even if you specify it like this, it seems to work well in most cases, but if you use an alias when accessing the server, you cannot reflect the alias name. I think that it is possible to use HTTP_HOST for the server name, but HTTP_HOST is not worth trusting (= header that the client can arbitrarily set), so I think it should be avoided.

From the above, I think that it is necessary to register a service in DI that judges whether HTTP_HOST is valid and returns a value, instead of using staticBaseUri.

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