This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

seanjkanderson58060
seanjkanderson58060

This must be trivial but I can't find a non-brittle solution to this: I need a way to access a file in my assets folder from the PHP section of my .htm file.

function onStart()
{
    // -- Matomo Tracking API init -- 
require_once "/path/to/MatomoTracker.php";
MatomoTracker::$URL = 'https://matomo.example.org/';
}

Here "/path/to/MatomoTracker.php" is my_theme_name/assets/MatomoTracker.php. How do we access the assets subdirectory without using twig and do so in a robust way?

Thanks!

mjauvin
mjauvin
$this->controller->themeUrl('assets/MatomoTracker.php');
mjauvin
mjauvin

I'm sorry, the answer I gave above is wrong. Use this instead:

$assetUrl = $this->controller->themeUrl('assets/MatomoTracker.php');
$assetPath = public_path(parse_url($assetUrl, PHP_URL_PATH));

require_once $assetPath;
mjauvin
mjauvin

Or, even simpler:

$assetPath = public_path($this->controller->getAssetPath('assets/MatomoTracker.php'));

require_once $assetPath;
mjauvin
mjauvin

Yet another way:

$asset = new \Cms\Classes\Asset($this->controller->getTheme());

require_once $asset->getFilePath('MatomoTracker.php');

Last updated

1-5 of 5

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.