- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
到目前为止,我所有的网站都是用非常线性的代码编写的(感谢 QBASIC!),这常常让我对重复、困惑和笨拙的代码感到悲伤。每个动态页面都有自己独立的 .php 文件,我从未使用过函数。
我现在正在使用 OOP 和 Smarty 作为模板引擎重写整个网站,并且确实需要一些指导。
这是我目前的流程:
1) 访客点击 index.php?/view/fluffybunny。 index.php 只是网站其余部分的处理程序:
// main config holds all database, sphinx and memcached settings
// and also connects to server
require_once '../config/site.cfg.php';
$urlPath = explode('/',$_GET['path']);
$page = new page($tpl,$db);
if (!empty($urlPath[1]))
{
switch ($urlPath[1])
{
case 'view': $page->view($urlPath); break;
case 'browse': $page->browse($urlPath); break;
case 'index': $page->index($urlPath); break;
default: $page->index($urlPath); break;
}
}
else
{
header('HTTP/1.0 404 Not Found'); // then 404 because we dont want seo issues
exit("File not found");
}
2) 'new page()' 然后触发自动加载器以包含 page.class.php:
/* page Class
* Created by James Napier 16/05/2013
* Revision 0.1
*/
class page
{
private $tpl;
private $db;
public function __construct($tpl='',$db='') {
$this->tpl = $tpl;
$this->db = $db;
}
/*
* Displays index page
* Depending on URL it either displays a local index or countrywide index
*/
public function index($urlPath)
{
$locUrl = '';
if (!empty($urlPath[3])) //this is the location part
{
// init location class
$location = new location($this->db);
// check whether location exists
if($location->checkByUrl($urlPath[3]))
{
$locUrl = '/in/'.$urlPath[3]; // if it does add location suffix to urls
}
else
{
// if it doesnt, 404
header('HTTP/1.0 404 Not Found');
exit("File not found");
}
}
// assign location url to template
$this->tpl->assign('locUrl', $locUrl);
// homepage breadcrumbs
$this->tpl->assign('breadCrumbs',array(
array('title'=>'Site '.COUNTRY_CODE1, 'friendlyurl'=>'/'),
array('title'=>'Home', 'friendlyurl'=>'/')));
// Build the template and display it
$this->tpl->display('index.tpl');
}
/*
* Displays individual listing
* Uses the ID from the end of the URL to display the correct item
*/
public function view($urlPath)
{
$id = end($urlPath);
if (!empty($id))
{
// Retrieve the article from the database along with POINT values
$stmt = $this->db->prepare('SELECT *, X(locpoint) as x, Y(locpoint) as y FROM listing WHERE id = :id LIMIT 1');
$stmt->bindValue(':id', $id, PDO::PARAM_INT);
$stmt->execute();
if($listing = $stmt->fetch(PDO::FETCH_ASSOC))
{
// Deal with the article status
if ($listing['status'] == 'deleted')
{
$this->err404();
}
elseif ($listing['status'] == 'disabled')
{
$this->tpl->assign('bannerErr','THIS AD HAS EXPIRED AND IS PENDING DELETION');
}
elseif ($listing['status'] == 'pending')
{
$this->tpl->assign('bannerErr','THIS AD IS NOT YET ACTIVE AND IS BEING REVIEWED BY THE FLOGR TEAM');
}
// Start building the basic page vars from the results
$this->tpl->assign('itemID', $listing['id']);
$this->tpl->assign('itemTitle',$listing['title']);
$this->tpl->assign('itemDescription',$listing['description']);
$this->tpl->assign('itemShortDesc',$listing['shortdesc']);
// price
$this->tpl->assign('itemPrice', number_format($listing['price']));
$this->tpl->assign('itemPriceTag',$listing['pricetag']);
// location details
$this->tpl->assign('itemLatLng', $listing['x'].','.$listing['y']);
$this->tpl->assign('itemLocation', $listing['loctext']);
// contact details
$this->tpl->assign('itemContactName',$listing['name']);
$this->tpl->assign('itemContactNo', $listing['phone']);
$this->tpl->assign('itemContactType', $listing['type']);
// images
$this->tpl->assign('itemImages', json_decode($listing['images'], true));
// Retrieve the category info for breadcrumbs and titles
$getContent = new getContent();
// breadcrumbs
$this->tpl->assign('breadCrumbs',$getContent->breadCrumbs($listing['catid'],'/browse/', $this->db));
// Page and SEO titls
$this->tpl->assign('headTitle',$listing['title'].' located in '.$listing['loctext'].' | site.com');
$this->tpl->assign('headDesc', $listing['shortdesc']);
// Build the template and display it
$this->tpl->display('view_ad.tpl');
// Update hits and set viewed session var
$_SESSION['filter']['viewed'][] = $listing['id'];
$stmt = $this->db->query('UPDATE LOW_PRIORITY listing SET hits = hits+1 WHERE id = '.$listing['id']);
}
else
{
$this->err404();
}
}
else
{
$this->err404();
}
}
/*
* standard 404 error
*/
public function err404()
{
header('HTTP/1.0 404 Not Found');
exit("File not found");
}
}
首先我的问题是,作为 OOP 新手,有人能看出我犯的任何明显错误吗?
其次,无论 index.php 处理程序是否调用了 $page->index()、$page->view,我都有一些大块代码需要在每个页面上运行(用户身份验证等) ()...ETC。我如何将此代码集成到 page.class.php 中?
最佳答案
就您的代码而言,您在 OOP 方面做得很好,但通过从头开始编写框架,您基本上是在重新发明轮子。我强烈建议使用现有的框架,例如:CakePHP、Zend Framework 或 Laravel。使用框架一段时间后,您就会开始掌握自己喜欢什么和不喜欢什么。在这一点上,制作自己的框架是一个很好的练习。但即使在那个时候,您也应该意识到,已经使用 100 甚至 1,000 小时的人力开发了开源框架,虽然每个框架可能都有您不喜欢的怪癖,但处理这些怪癖是比从头开始构建框架要容易得多。
关于php - 在 OOP 中重写整个网站,我这样做是不是错了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16822669/
我无法在附加行中显示“真”、“假”、"is"和“否”按钮。 我在这里有一个应用程序:Application 请按照以下步骤使用应用程序: 1。当你打开应用程序时,你会看到一个绿色的加号按钮,点击 在此
我是一名优秀的程序员,十分优秀!