【发布时间】:2015-04-29 05:56:00
【问题描述】:
我在 github 上找到了这个 php 项目。 Vanilla Kit [https://github.com/syndicatefx/vanilla-kit] ,是一个非常简单的 php 驱动的动态网站模板,我喜欢干净的文件夹结构,因此决定使用/尝试它。
这是根目录上的索引页面,非常简单。它调用用户请求的页面(在文件夹 pages 中)并将变量 $page 替换为请求的页面名称并显示它。
<?php
// Defualt page will always be pages/homepage.html, if not, change this to the name of the file you have created to be the homepage.
$page = 'homepage';
// Get pages based on user input
if (!empty($_GET['name'])) {
//Assign a variable to a sanitised version of the data passed in the URL
$tmp_page = basename($_GET['name']);
//If the file exists, update $page
if (file_exists("pages/{$tmp_page}.php"))
$page = $tmp_page;
//If the file does not exist, include notfound page and exit
elseif(!file_exists($tmp_page)){
include 'pages/notfound.php';
exit;
}
}
// Include $page (declared default)
include ("pages/$page.php");
?>
作为主页的默认页面从产品表中获取产品并显示它。
<?php
// Edit this page's title, description and keywords for SEO
$pagetitle = 'Welcome';
$pagedescription = 'description goes here...';
$pagekeywords = 'keywords,go,here';
// Add a class to body for more CSS power
$bodyclass = 'home';
// Do Not Remove
include 'inc/header.php';
?>
<?php
$dbquery = "SELECT * FROM lbtbl_products";
$productresult = $dbconnect->query($dbquery);
if ($productresult->num_rows > 0) {
while($row = $productresult->fetch_assoc()) { ?>
<div class="prod-cnt prod-box">
<form method="post" action="cartupdate.php">
<h3 class="prod-title">
<a href="productdetail.php?id=<?php echo $row['lbproductId'];?>"><?php echo $row["lbproductName"]; ?></a>
</h3>
<p><?php echo $row["lbproductDescription"];?></p>
<div class="price-cnt">
<div class="prod-price"><img src="images/common/rupees.png" width="7" height="10"/> <?php echo $row["lbproductPrice"];?></div>
Qty <input type="text" name="product_qty" value="1" size="3" />
<button class="add_to_cart">Add To Cart</button>
<input type="hidden" name="product_code" value="<?php echo $row["lbproductSku"];?>" />
<input type="hidden" name="type" value="add" />
<input type="hidden" name="return_url" value="<?php echo $current_url;?>" />
</div>
</form>
</div>
<?php }
} else {
echo "0 results";
}
$dbconnect->close(); ?>
<?php include 'inc/footer.php'; ?>
到目前为止一切正常。
现在,为了显示产品详细信息,我有一个页面名称 productdetails.php,它与所有其他页面一样保存在 pages 目录中。 “主页”上查看产品详细信息的链接是
<a href="productdetail.php?id=<?php echo $row['lbproductId'];?>"><?php echo $row["lbproductName"]; ?></a>
但是一旦点击就会显示 404 not found 页面。但是,如果我将 productdetails.php 页面移动到根目录,它就可以工作。任何人都可以帮助/建议解决方案。我最好的猜测是它与注释后的 index.php 代码有关 // Get pages based on user input.
【问题讨论】:
标签: php url url-rewriting url-routing