这是使用 PHP 创建动态网页的好方法吗?
我正在尝试使用 PHP 中的 include()
创建动态网页。 该 PHP 页面具有以下代码 - 从字面上看,这就是文件包含的全部内容:
<?php
session_start();
$dbName = $_REQUEST['DBName'];
$tbName = $_REQUEST['TBName'];
$dbType = $_REQUEST['DBType'];
include('header.php');
switch($dbType)
{
case 'Calender':
include('CalenderPage.php');
exit;
case 'News':
include('NewsPage.php');
exit;
case 'Gallery':
include('GalleryPage.php');
exit;
}
include('footer.php');
?>
您认为这是创建动态 PHP 页面的好方法吗?
I'm trying to create a dynamic webpage using include()
in PHP.
This PHP page has the following codes - literally, that's all the file contains:
<?php
session_start();
$dbName = $_REQUEST['DBName'];
$tbName = $_REQUEST['TBName'];
$dbType = $_REQUEST['DBType'];
include('header.php');
switch($dbType)
{
case 'Calender':
include('CalenderPage.php');
exit;
case 'News':
include('NewsPage.php');
exit;
case 'Gallery':
include('GalleryPage.php');
exit;
}
include('footer.php');
?>
Do you think it's a good way of creating dynamic PHP page?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你所展示的完全没问题。使用数组可以更简单一些:
What you show is perfectly fine. It could be done a bit simpler using an array:
就我个人而言,我不喜欢
include()
函数来创建动态 php 页面。我发现很难避免变量在不同的 php 文件中发生冲突。假设您将在NewsPage.php
文件中更改$dbName
,这可能会导致您的主页出现一些问题,并且您可能没有意识到出了什么问题。我只包含带有类/函数的文件,这样我就可以控制包含的 php 文件中正在执行的代码,并且更容易调试我的代码。
Personally, I don't like the the
include()
function to create dynamic php page. I find it very difficult to avoid variables beeing conflicted in different php files. Let say that inNewsPage.php
file you will change$dbName
, it could result some problems in your main page and you might not realize what went wrong.I only include files with classes/functions, that way i can control what code is beeing executed in the included php files, and it's more easy to debug my code.
对于初学者来说这没问题,但请注意您的 DRY 问题,您添加的每个页面都将另外编写 3 行。
就这样维持下去是不可能的。
我看到您在 dbType 和 page 之间有一个名称约定,请尝试:
而不是 switch 块
It's fine for starters, but notice your DRY issue, each page you'll add, you will write another 3 lines.
It'll be impossible to maintain it this way.
I see you got a name convention between the dbType and page, try :
instead of switch block
这种风格的页面的名称是模板。它似乎与选择要显示的内容的任何其他方法一样有效。
The name for this style of page is a template. It seems as valid as any other way to choose which content to display.