简体   繁体   English

Magento-在phtml模板文件中包含/需要php文件

[英]Magento - include/require php file within phtml template file

In one of my magento template phtml files I am trying to include a seperate php file. 在我的magento模板phtml文件之一中,我试图包括一个单独的php文件。 When I include it I get nothing outputted and when i use require instead I get the following error 当我包含它时,我什么也没输出,而当我使用require时,我得到了以下错误

Fatal error: require(): Failed opening required 'http://www.site.co.uk/dir/test.php'
(include_path='/home/usr/public_html/app/code/local:/home/usr/public_html/app/code/community:/home/usr/public_html/app/code/core:/home/usr/public_html/lib:.:/usr/lib/php:/usr/local/lib/php') in /home/usr/public_html/app/design/frontend/theme/edits/template/review/product/view/list.phtml on line 30

The first line of the error shows the correct url path and when i go to it directly it works - it just doesn't like being included/required from the phtml template page. 错误的第一行显示了正确的url路径,当我直接转到它时,它可以工作-只是不喜欢从phtml模板页面中包含/要求它。

I've tried the following in the phtml file (using magento's BaseURL, absolute path and the relative path): 我在phtml文件中尝试了以下操作(使用magento的BaseURL,绝对路径和相对路径):

<?php 
$root = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
require/include ($root.'dir/test.php');
?>

<?php 
require/include ('http://www.site.co.uk/dir/test.php');
?>

<?php 
require/include ('../../../../../../../../../dir/test.php');
?>

代替Mage::getBaseUrl使用$root = Mage::getBaseDir();

require('http://....') is going to (if it's enabled) do a full-blown HTTP request to the specified URL. require('http://....')将对指定的URL进行完整的HTTP请求(如果已启用)。 Since it's a URL, that webserver is going to EXECUTE the php script and send you its output. 由于这是一个URL,因此该网络服务器将执行 php脚本并将其输出发送给您。

If you're trying to actually load the CODE of that test.php , eg the raw un-executed PHP, then you cannot use an http request. 如果您试图实际加载该test.php的CODE(例如未执行的原始PHP),则不能使用http请求。 The remote server has NO idea that the http request is actually from PHP and is coming from in include(). 远程服务器不知道http请求实际上来自PHP,并且来自include()。 It'll just blindly run that script and send over the output. 它只会盲目地运行该脚本并发送输出。

You'd need to (say) rename the remote file to test.txt , so that the webserver sends it out as-is, and then that text will be executed as PHP code on YOUR server. 您需要(例如)将远程文件重命名为test.txt ,以便网络服务器按原样发送它,然后该文本将作为PHP代码在您的服务器上执行。

And as far as the other paths go, if your $root is something like: 至于其他路径,如果您的$root是这样的:

 /home/sites/example.com/html

then the require is going to be looking for 然后需求将要寻找

 /home/sites/example.com/html/dir/test.php

Is there a dir subdir inside whatever your site's $root is? 站点的$root内是否有dir子目录?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM