Php 如果我有这段代码作为我的控制器,如何创建url

Php 如果我有这段代码作为我的控制器,如何创建url,php,url,url-rewriting,Php,Url,Url Rewriting,我的所有网页homepage.php、about.php、contact.php都在一个文件夹pages中。根目录上的index.php根据用户输入控制要加载的页面 <?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'; // G

我的所有网页homepage.php、about.php、contact.php都在一个文件夹pages中。根目录上的index.php根据用户输入控制要加载的页面

<?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");
?>

index.php上的默认页面是homepage.php,它从products表中获取产品并显示它

文件夹页面中的homepage.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" />
    </div>
</form>
</div>
<?php }
    } else {
    echo "0 results";
    }
    $dbconnect->close(); ?>

数量 添加到购物车 使用此类型的URL www.abc.com/productdetails?product_id=1

然后,您的路由系统搜索productdetails.php就存在于pages目录中。如果存在,则加载页面,然后您可以使用产品id$\u GET['product\u id']查找产品


也许它会起作用

您使用.htaccess吗?您的项目url是这样的吗?@Nahid,是的,它使用.htaccess-Nahid。否主页设置为默认,但后续页面会显示为www.abc.com/contact