我刚开始编写 PHP,我知道如何包含 PHP,但是在这种情况下我遇到了困难。
我有一个 LAMP 服务器,在根文件夹中我安装了自己的框架,如下所示(文件夹以大写字母表示)。
index.php
page.php
INCLUDES
- classes.php
- skins.php
- flow_init.php
- flow_head.php
- flow_body.php
SOURCES
- page1.php
- page2.php
...
THEME_HTML
- wrapper.html
VIEWS
- page1.html
- page2.html
...
wrapper.html
被创建成 index.php
<?php // index.php
$skin = new skin('wrapper');
echo $skin->make();
这是我的
skins.php
文件<?php // INCLUDES/skins.php
class skin {
var $filename;
public function __construct($filename) {
$this->filename = $filename;
}
public function mk($filename) {
$this->filename = $filename;
return $this->make();
}
public function make() {
global $CONF;
$file = sprintf('./'.$CONF['theme_path'].'/'.$CONF['theme_name'].'/html/%s.html', $this->filename);
$fh_skin = fopen($file, 'r');
$skin = @fread($fh_skin, filesize($file));
fclose($fh_skin);
return $this->parse($skin);
}
private function parse($skin) {
global $TMPL, $LNG;
$skin = preg_replace_callback('/{\$lng->(.+?)}/i', create_function('$matches', 'global $LNG; return $LNG[$matches[1]];'), $skin);
$skin = preg_replace_callback('/{\$([a-zA-Z0-9_]+)}/', create_function('$matches', 'global $TMPL; return (isset($TMPL[$matches[1]])?$TMPL[$matches[1]]:"");'), $skin);
return $skin;
}
}
?>
VIEWS
的每一页都在 SOURCES
中创建,page.php
对其进行回显。集成 的脚本
一切正常,但现在我需要在包装器(头部标记所在的位置)和一个 View (内容所在的位置)之间集成此脚本
<?php
session_start();
require_once('admin/plugins/flow-flow/ff-injector.php');
$injector = new FFInjector();
?>
<!DOCTYPE html>
<html lang="en-US">
<head><?php echo $injector->head(true,true); ?>
</head>
<body>
<table width="100%">
<tr>
<td style="width: 300px;vertical-align: top">
<?php
$stream_id = isset($_REQUEST['stream']) ? $_REQUEST['stream'] : 1;
$injector->stream($stream_id);
?>
</td>
</tr>
</table>
</body>
</html>
为此,我尝试创建一些模板:
<?PHP // index.php
$TMPL['flow_init'] = include './includes/flow_init.php';
$TMPL['flow_head'] = include './includes/flow_head.php';
<?PHP // SOURCES/page1.php
$TMPL['flow_body'] = include './includes/flow_body.php';
我包含的文件设置如下
<?PHP // flow_init.php
session_start();
require_once('admin/plugins/flow-flow/ff-injector.php');
$injector = new FFInjector();
?>
<?PHP // flow_head.php
echo $injector->head(true,true);
?>
<?PHP // flow_body.php
$stream_id = isset($_REQUEST['stream']) ? $_REQUEST['stream'] : 1;
$injector->stream($stream_id);
?>
我期望实现的目标
我试图在我的包装器上包含
flow_init
和 flow_head
,如下所示:// THEME_HTML/wrapper.html
{$flow_init}
<!DOCTYPE html>
<html lang="en">
<head>
{$flow_head}
</head>
<body>
<div id="content" class="wrapper">
{$content}
</div>
</body>
</html>
并将
flow_body
添加到我的内容中// THEME_HTML/VIEWS/page1.html
<div class="row-body{$content_class}">
<div class="body-content">
<div class="nine columns" id="main-content">
{$flow_body}
</div>
<div class="three columns">
{$sidebar}
</div>
</div>
</div>
结果是一个空白页,但是如果我将所有 3 个模板一起包含在
index.php
中,它就可以工作,所以我认为这不是源路径问题。划分 PHP 脚本并使其可执行的正确方法是什么?
最佳答案
如果您想开始在文件中包含文件,您需要学习 PSR-4 自动加载以及如何使用 Composer。这将比在您希望包含文件的任何地方使用 require_once 更容易。
如果您在谈论 View 模板,请不要重新发明轮子,除非您想了解有关轮子的更多信息。我用过的两个最好的模板引擎是 Laravel's Blade 和 Symfony's Twig。您还可以尝试使用仅使用原始 PHP 的 PHP 开发人员联盟的模板引擎。
关于php - 在另一个脚本中划分和集成 PHP 脚本的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36495445/