需要 PHP 技术架构师的意见
请看下面的代码。
require_once("initvars.inc.php");
require_once("config.inc.php");
?>
<?php
if($latestads_count)
{
?>
<div class="latestposts">
<div class="head"><?php echo $lang['LATEST_ADS']; ?></div>
<table border="0" cellspacing="0" cellpadding="0" class="postlisting" width="100%">
<?php
$sql = "SELECT a.*, ct.cityname, UNIX_TIMESTAMP(a.createdon) AS timestamp, feat.adid AS isfeat,
COUNT(*) AS piccount, p.picfile AS picfile, scat.subcatname, scat.catid, cat.catname
FROM $t_ads a
INNER JOIN $t_cities ct ON a.cityid = ct.cityid
INNER JOIN $t_subcats scat ON a.subcatid = scat.subcatid
INNER JOIN $t_cats cat ON scat.catid = cat.catid
LEFT OUTER JOIN $t_featured feat ON a.adid = feat.adid AND feat.adtype = 'A' AND feat.featuredtill >= NOW()
LEFT OUTER JOIN $t_adpics p ON a.adid = p.adid AND p.isevent = '0'
WHERE $visibility_condn
$loc_condn
GROUP BY a.adid
ORDER BY a.createdon DESC
LIMIT $latestads_count";
$res_latest = mysql_query($sql) or die($sql.mysql_error());
$css_first = "_first";
while($row = mysql_fetch_array($res_latest))
{
$url = buildURL("showad", array($xcityid, $row['catid'], $row['catname'],
$row['subcatid'], $row['subcatname'], $row['adid'], $row['adtitle']));
?>
<?php
if($row['isfeat'])
{
//$feat_class = "class=\"featured\"";
$feat_img = "<img src=\"images/featured.gif\" align=\"absmiddle\">";
}
else
{
//$feat_class = "";
$feat_img = "";
}
if($row['picfile'])
{
$picfile = $row['picfile'];
$imgsize = GetThumbnailSize("{$datadir[adpics]}/{$picfile}", $tinythumb_max_width, $tinythumb_max_height);
}
else
{
$picfile = "";
}
?>
<tr>
<td width="15">
<img src="images/bullet.gif" align="absmiddle">
</td>
<td>
<b><a href="<?php echo $url; ?>" <?php echo $feat_class; ?>><?php echo $row['adtitle']; ?></a></b>
<?php if(0&&$row['picfile']) { ?><img src="images/adwithpic.gif" align="absmiddle"><?php } ?>
<?php echo $feat_img; ?><br>
<span class="adcat">
<?php echo "$row[catname] $path_sep $row[subcatname]"; ?>
<?php
$loc = "";
if($row['area']) $loc = $row['area'];
if($xcityid < 0) $loc .= ($loc ? ", " : "") . $row['cityname'];
if($loc) echo "<br>$loc";
?>
</span>
</td>
<td align="right" width="<?php echo $tinythumb_max_width; ?>">
<?php if($picfile) { ?>
<a href="<?php echo $url; ?>"><img src="<?php echo "{$datadir[adpics]}/{$picfile}"; ?>" border="0" width="<?php echo $imgsize[0]; ?>" height="<?php echo $imgsize[1]; ?>" style="border:1px solid black"></a>
<?php } ?>
</td>
</tr>
这是现有项目的代码文件之一。正如你所看到的,它有 html、sql、php 混合在一起......并且显然很难维护。
此应用程序中有大约 55 个大小和类型相似的文件。
我想重构这段代码,以下是我这样做的目标:
1)易于维护。
2)能够通过添加附加功能轻松扩展。
3)能够为不同但有些相似的应用程序重用此代码。
基于上述事实,我有很多问题:
1)你认为我们可以将这段代码重构为 mvc 应用程序吗?
2)如果我们可以的话。对于一个专家程序员来说,重构整个项目(55 个文件)需要多少时间?
3)我应该重用上面的代码,还是应该从头开始使用现有的 mvc 框架?
4)如果我们使用现有的 mvc 框架(例如 symfony、zend 等),我们需要多少时间才能完成整个项目?
5)据我所知,到目前为止,该站点仅运行Mysql(因为当前的数据库是在mysql中完成的)。我们是否应该在模型中允许数据抽象/数据访问层(假设有比Mysql性能更好的东西,我不确定这一点)
7)我们可以轻松地重构代码以包含模型子层(数据抽象) /access)、视图(进一步的模板、视图逻辑等)、控制器等,以防我们将来想要这样做,或者我们需要从头开始?
8)mvc 是可行的方法,还是有比它更好的模式/方法(假设该网站面向数十万用户)?
Please look at the code below.
require_once("initvars.inc.php");
require_once("config.inc.php");
?>
<?php
if($latestads_count)
{
?>
<div class="latestposts">
<div class="head"><?php echo $lang['LATEST_ADS']; ?></div>
<table border="0" cellspacing="0" cellpadding="0" class="postlisting" width="100%">
<?php
$sql = "SELECT a.*, ct.cityname, UNIX_TIMESTAMP(a.createdon) AS timestamp, feat.adid AS isfeat,
COUNT(*) AS piccount, p.picfile AS picfile, scat.subcatname, scat.catid, cat.catname
FROM $t_ads a
INNER JOIN $t_cities ct ON a.cityid = ct.cityid
INNER JOIN $t_subcats scat ON a.subcatid = scat.subcatid
INNER JOIN $t_cats cat ON scat.catid = cat.catid
LEFT OUTER JOIN $t_featured feat ON a.adid = feat.adid AND feat.adtype = 'A' AND feat.featuredtill >= NOW()
LEFT OUTER JOIN $t_adpics p ON a.adid = p.adid AND p.isevent = '0'
WHERE $visibility_condn
$loc_condn
GROUP BY a.adid
ORDER BY a.createdon DESC
LIMIT $latestads_count";
$res_latest = mysql_query($sql) or die($sql.mysql_error());
$css_first = "_first";
while($row = mysql_fetch_array($res_latest))
{
$url = buildURL("showad", array($xcityid, $row['catid'], $row['catname'],
$row['subcatid'], $row['subcatname'], $row['adid'], $row['adtitle']));
?>
<?php
if($row['isfeat'])
{
//$feat_class = "class=\"featured\"";
$feat_img = "<img src=\"images/featured.gif\" align=\"absmiddle\">";
}
else
{
//$feat_class = "";
$feat_img = "";
}
if($row['picfile'])
{
$picfile = $row['picfile'];
$imgsize = GetThumbnailSize("{$datadir[adpics]}/{$picfile}", $tinythumb_max_width, $tinythumb_max_height);
}
else
{
$picfile = "";
}
?>
<tr>
<td width="15">
<img src="images/bullet.gif" align="absmiddle">
</td>
<td>
<b><a href="<?php echo $url; ?>" <?php echo $feat_class; ?>><?php echo $row['adtitle']; ?></a></b>
<?php if(0&&$row['picfile']) { ?><img src="images/adwithpic.gif" align="absmiddle"><?php } ?>
<?php echo $feat_img; ?><br>
<span class="adcat">
<?php echo "$row[catname] $path_sep $row[subcatname]"; ?>
<?php
$loc = "";
if($row['area']) $loc = $row['area'];
if($xcityid < 0) $loc .= ($loc ? ", " : "") . $row['cityname'];
if($loc) echo "<br>$loc";
?>
</span>
</td>
<td align="right" width="<?php echo $tinythumb_max_width; ?>">
<?php if($picfile) { ?>
<a href="<?php echo $url; ?>"><img src="<?php echo "{$datadir[adpics]}/{$picfile}"; ?>" border="0" width="<?php echo $imgsize[0]; ?>" height="<?php echo $imgsize[1]; ?>" style="border:1px solid black"></a>
<?php } ?>
</td>
</tr>
This is one of the code files from an existing project. As you can see, it has html, sql, php all intermixed together... and obviously difficult to maintain.
There are around 55 files of similar size and type in this application.
I want to refactor this code,and below are my objectives for doing so:
1)Easy to maintain.
2)Be able to extend easily by adding additional features.
3)Be able to reuse this code for different but somewhat similar applications.
I have a number of question based on the above facts:
1) Do you think we can refactor this code into an mvc application?
2)If we can. how much time should it take to refactor the whole project(55 files) say, for an expert programmer?
3)Should i reuse the above code, or should i start from scratch with a existing mvc framework?
4)How much time do we need to complete the whole project, in case we go for an existing mvc framework say symfony, zend etc.?
5)This site as per my knowledge until now would run Mysql only(as the current db is done in mysql). Should we allow for data abstraction/data acces layers in the model(supposing there is something that performs better than Mysql, I'm not sure about this)
7)Can we easily refactor the code to include sub-layers for model(data abstraction/access), view(further templates, view logic etc.), controller etc. in case we want to do that in future or would we need to start from scratch?
8)Is mvc the way to go , or is there a better pattern/way than it(supposing the site is intended for hundred thousands of users)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为不存在“mvc应用程序”这样的东西,但是mvc模式肯定可以在这个应用程序中使用。您可以构建一些数据类来封装各种查询并将结果转换为一个对象或对象数组。控制器类可以获取这些结果并对其进行处理,然后将处理后的数据提供给特定的模板。这是一个类似 mvc 的结构,非常容易维护。我也会构建一些辅助类。您不想到处调用 mysql_query,因为明年您可能需要使用 mysqli,甚至可能需要使用完全不同的数据库。
所以,是的,使用 mvc 模式,但不仅如此。重构整个代码。尽可能使用现成的库(例如数据库抽象),并在开始构建之前花大量时间思考正确的方法。
I think there is no thing like an "mvc application", but the mvc pattern can certainly be used in this application. You could build some data classes that encapsule the various queries and translate the result to an object or arrays of objects. A controller class can grab those results and process them, then feed the processed data to a specific template. That is an mvc like construct that is quite easy to maintain. I'd build some helper classes as well. You don't want to call mysql_query everywhere, because maybe you'll need to use mysqli next year, or maybe even a completely different database.
So, yes, use the mvc pattern, but not only that. Refactor the entire code. Use ready to use libraries where you can (for instance for database abstraction), and spent a great deal thinking about the right way to go before you start building.