1

I am generating a url within Element API:

'profile' => UrlHelper::getUrl("directory/company/{$member->id}/{$urlslug}"),

which outputs:

http:\/\/craft.fira\/directory\/company\/1501\/abet-limited

What do I need to do to turn this into a real url without the backwards slashes "\"? As far as I understand this is a regular expression (regex) version of the url?

I will be using javascript to put this on the page, so it could either be fixed at source in the Element api php code, or alternatively when scraping through the JSON with Javascript.

Thanks in advance guys! :-)

2

The plugin probably uses PHP's json_encode to serialize the data and it automatically escapes quotation marks and backslashes to conform to the JSON format. Escaping the forward slashes of your URL wouldn't actually be necessary, but I just found this on StackOverflow which might give an idea why the function escapes them anyways:

https://stackoverflow.com/questions/1580647/json-why-are-forward-slashes-escaped

But you shouldn't really care about this behavior at all, because however you gonna consume that JSON string, the parser (e.g., jQuery.parseJSON()) should handle the unescaping for you.

1
  • Great. Sorry - newb questions... So I won't need to alter what is being output now anyway, all should run smoothly by the sounds of things. Thank you so much for your help – Adam Menczykowski Jan 29 '16 at 12:44

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.