Ad

CakePHP4: Link To Json File In Script

- 1 answer

I have a CakePHP4 App where I create a D3JS WorldMap in a <script> tag. I need to link to a JSON File in this line d3.json("https://api.jsonbin.io/b/61f8ec14518e5f3b2ab3e218", function(error, topology) { but this time its saved in webroot folder.

I have tried to get the Path console.log('<?php echo WWW_ROOT; ?>'); but it didnt work. How can I set the Path to my file over JavaScript?

Ad

Answer

How can I set the Path to my file over JavaScript?

path: "<?= $this->Url->build('/my.json', ['fullBase' => true]); ?> "

// Output
// path: "http://somedomain.com/my.json"

or:

<div data-json="<?= $this->Url->build('/my.json', ['fullBase' => true]); ?>" ></div>

then in your js read data attribute

Read more: https://book.cakephp.org/4/en/views/helpers/url.html

Ad
source: stackoverflow.com
Ad