I'm trying to use file_get_contents
to reference a .json
file in my module.
My directory structure is as follows
my custom field which is trying to reference the json file
/models/fields.stackexchangesites.php
my json file
/media/js/sites.json
If I use
$json = 'modules/mod_kou_stack_exchange/media/js/sites.json';
$json = file_get_contents($json);
PHP cannot find the file. I think perhaps because it is trying to reference it from the original file location. If I put the full location in the file it works, so long as I add an exception in Admin Tools. I don't want to hard code the URL as it might be used on other sites.
So then I have the option to use the URI function in Joomla.
https://docs.joomla.org/URLs_in_Joomla
However, if I try to use
use Joomla\CMS\Uri\Uri;
$uri = Uri::getInstance();
$url = $uri->toString();
within my code, it is then not accessible within the class that I am using
class JFormFieldStackexchangesites extends JFormFieldList
If I try to add use
$uri
and $url
within the class that doesn't work either.
I'm sure this must be something to do with scope but having tried a few different ways I can't understand how to get it working.
Can anyone help me to understand how to get this working? And can anyone help me to understand why a relative path doesn't work?
ROOT/media/...
orROOT/modules/mod_kou_stack_exchange/media/...
?