1

I'm trying to get url from request,

 {{ craft.app.request.url }}

but the uri is chinese, like "localhost/測試",

it print out "http://localhost/����"

did i miss some setting in craft CMS ?

Thank you.

5
  • localhost/����
    – ivesho
    Mar 25, 2021 at 8:05
  • Guessing you need to set <meta charset="UTF-8"> in your template?
    – Brad Bell
    Mar 25, 2021 at 22:56
  • @BradBell Thanks your help! I trid, but not working.
    – ivesho
    Mar 29, 2021 at 6:08
  • Maybe a server-side thing? If you go to Utilities->PHP Info in the Craft control panel and search for default_charset, what is it set to?
    – Brad Bell
    Mar 29, 2021 at 23:13
  • The setting is "UTF-8". I try Request class methods, the getAbsoluteUrl() has same problem, "localhost����". But getFullPath(), getFullUri() and getSegment(1) can show chinese, "測試" . Sorry I am new in Craft CMS and programing.
    – ivesho
    Mar 30, 2021 at 2:27

1 Answer 1

0

This isn't really an answer, but my investigating was too long for a comment.

Ultimately, craft.app.request.url gets routed through Yii's resolveRequestUri() and likely gets pulled from $_SERVER['REQUEST_URI'].

Anything coming from $_SERVER['REQUEST_URI'] is always going to be URL encoded so using this in a template:

enter image description here

I get this URL encoded output in a browser:

enter image description here

Unfortunately, Twig doesn't have a |url_decode filter, so you'd need to write a quick custom Twig filter that uses PHP's urldecode() method.

But it actually looks like you're having a character encoding issue first because of the ���� and I'm not exactly sure what's causing that.

My guess is its environment configuration related (I'm using https://getnitro.sh in a fairly default setup for testing).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.