Im trying to autoload php from within a template. I have php enabled, but I’m having problems with the path following ‘require_once’. Expression Engine automatically adds ‘ee/legacy/libraries’ to the beginning of the path, which makes
require_once DIR . ‘/folder/vendor/file.php’;
require_once DIR . ‘ee/legacy/libraries/folder/vendor/file.php’;
which of course breaks the path. How do I tell EE to stop doing this?
Generally speaking we discourage the use of PHP in templates. We would recommend going the plugin route (or another add-on option route). If your using version 6 I can shoot over a video that will make it quick to get started with a plugin if you would like?
(FYI, I haven’t tested this) My gut reaction is that this is the current working directory at the time of template processing. You could confirm this with getcwd. So if you choose to go the php in template route, you would likely need to account for this with either adding ../../../ (etc) or changing the present working directory.
Try using the full absolute path first and see if it works, such as:
require_once DIR . ‘/home/server/domain/folder/vendor/file.php’;
If it works, then start to try how to get there (it might require multiple tries):
require_once DIR . ‘../folder/vendor/file.php’;
require_once DIR . ‘/../folder/vendor/file.php’;
require_once DIR . ‘./../folder/vendor/file.php’;
require_once DIR . ‘../../folder/vendor/file.php’;
I think you get the idea. At some point you will get the relative path.