Escaping / Encoding URLs

Hi Everyone

Escaping / encoding. It always confuses me.

I am writing $page->url() and $file->url() into my page header sharing meta tags. For example:

<meta property="og:image" content="<?php echo $image->url() ?>" />

I think I should be escaping / encoding the URL. I am escaping text added to the meta tags using ->escape(). I cannot add this to $file->url().

I feel like this is a really dumb question, but hey …

How do I escape the URL? Do I need to escape the URL (or does Kirby do this already)?

Thank you
Regards
Mike

You don’t need to escape file URLs. Kirby automatically runs the image filename through rawurlencode so that spaces and other invalid characters are converted to percent encoded characters.

BTW: The reason why ->escape() does not work is that the url() method returns an URL string instead of a Field object. Only these objects have field methods.

1 Like