Magento无法获取require_once php文件

dzhpxtsq  于 2022-11-12  发布在  PHP
关注(0)|答案(3)|浏览(134)

我试图require_once一个php文件,这样我就可以使用文件中的函数了.无论我把php文件放在哪里,或者我试图在哪里请求它,都会发生同样的错误:

一月一日

PHP文件只包含一个函数,所以问题不是出在php文件本身,就像PHP找不到我的php文件一样,我试着这样要求它:

  1. <?php require_once($this->getBaseUrl() . 'functions.php');?>

最初我希望文件在**skin/theme/default/inc/**中,但出于测试目的,我将其移到了根目录中。
我已经检查了我的php.ini中是否有allow_url_fopen = On,但是我不知道这是否会影响require_once方法。
欢迎您提出任何建议。提前感谢:)

dgiusagp

dgiusagp1#

你不能在http上包含php文件。2这是错误的:

  1. required once 'http://example.com/some_file.php'

我想这就是你要找的:

  1. require_once($this->getBaseDir() . 'functions.php'

但这也不是Magento的做法。你应该创建一个模块,并将函数放在一个助手中。然后你就可以调用函数,而不需要像下面这样的require_once语句:Mage::helper('helper_alias_here')->doSomething()

编辑*有点离题 *

如果你使用Magento的正确方式,你不需要使用require语句,除非你正在重写一个控制器类。

yvgpqqbh

yvgpqqbh2#

getBaseUrl函数用于获取URL,而不是PATH

  1. Mage::getBaseDir()

此函数返回右magento安装路径

eanckbw9

eanckbw93#

试用此代码

  1. <?php
  2. namespace VendoreName\ModuleName\Controller\Index;
  3. use Magento\Framework\App\Action\Action;
  4. use Zend_Loader;
  5. class GetPdfData extends \Magento\Framework\App\Action\Action
  6. {
  7. /**
  8. * @var \Magento\Framework\View\Result\PageFactory
  9. */
  10. protected $resultPageFactory;
  11. /**
  12. * @var \Magento\Framework\Filesystem\DirectoryList
  13. */
  14. protected $dir;
  15. /**
  16. * @param \Magento\Framework\App\Action\Context $context
  17. * @param \Magento\Framework\Filesystem\DirectoryList $dir
  18. * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
  19. */
  20. public function __construct(
  21. \Magento\Framework\App\Action\Context $context,
  22. \Magento\Framework\Filesystem\DirectoryList $dir,
  23. \Magento\Framework\View\Result\PageFactory $resultPageFactory
  24. ) {
  25. $this->dir = $dir;
  26. $this->resultPageFactory = $resultPageFactory;
  27. parent::__construct($context);
  28. $this->initializePDF();
  29. }
  30. /**
  31. * Construct initialization of mpdf library
  32. */
  33. protected function initializePDF()
  34. {
  35. error_reporting(0);
  36. $path = $this->dir->getRoot() . "/vendor/mpdf/mpdf/src/";
  37. $this->pdf = $path . 'Mpdf.php';
  38. //include_once $this->pdf;
  39. Zend_Loader::loadFile($this->pdf, null, true);
  40. $this->configvariables = $path . 'Config/ConfigVariables.php';
  41. // require_once $this->configvariables;
  42. Zend_Loader::loadFile($this->configvariables, null, true);
  43. }
  44. /**
  45. * @inheritdoc
  46. */
  47. public function execute()
  48. {
  49. echo "Your Code";
  50. exit();
  51. }
  52. }

注意:此处需要添加要包含_once或require_once得文件得绝对路径. Mpdf是通过composer require mpdf/mpdf命令安装得,此模块放置在供应商目录中.

展开查看全部

相关问题