在另一个PHP脚本中划分和集成PHP脚本的正确方法是什么?
发布时间:2020-12-13 22:26:10 所属栏目:PHP教程 来源:网络整理
导读:我刚开始编写 PHP代码,我知道如何包含 PHP但是在这种情况下我遇到了困难. 我有一个LAMP服务器,在根文件夹中我安装了我自己的框架,如下所示(文件夹是大写字母). index.phppage.phpINCLUDES - classes.php - skins.php - flow_init.php - flow_head.php - flow
|
我刚开始编写
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_]+)}/','global $TMPL; return (isset($TMPL[$matches[1]])?$TMPL[$matches[1]]:"");'),$skin);
return $skin;
}
}
?>
VIEWS的每个页面都在SOURCES内创建,而page.php则回显它. 要集成的脚本 一切正常,但现在我需要在包装器(头标签所在的位置)和一个视图(内容所在位置)之间集成此脚本 <?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比希望包含文件更容易.
如果您正在谈论视图模板,请不要重新发明轮子,除非您想要了解有关轮子的更多信息.我用过的两个最好的模板引擎是Laravel的Blade和Symfony的Twig.您还可以尝试使用PHP开发人员的模板引擎,该引擎仅使用原始PHP. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
