格式化基本PHP网站的最佳方法



所以我想知道人们认为构建基本PHP网站的更好方法是什么。我有两个选项如下:

  1. www.website.com/index.php 或 www.website.com/contact-us.php

  2. www.website.com/index.php 或 www.website.com/index?contact-us

因此,在第一个选项中,网站的每个页面都有单独的页面,我将"include(("页眉和页脚,其余的将在该页面上创建。

在第二个选项中,只有一个索引页面,根据 $_GET 请求,所有内容都包含在该页面上。它看起来有点像这样

<?php include('inc/templates/header.php'); ?>
if(isset($_GET['home'])){
    include("pages/home.php");
}
if(isset($_GET['contact'])){
    include("pages/contact.php");
}
<?php include("inc/templates/footer.php"); ?>

对更好的方法有什么意见吗?

在我看来,最好的方法是实现基本的HTTP路由。这是服务器配置和请求解析的组合。例如,您可以将第三方库用于您的应用程序或获取编写自己的库的灵感。

路径将如下所示: site.com/route-name/foo/bar

例如,您可以查看:

https://github.com/dannyvankooten/PHP-Router

https://github.com/nikic/FastRoute

在Laravel框架中非常好的HTTP路由实现:

https://github.com/laravel/framework/tree/5.5/src/Illuminate/Routing

https://laravel.com/docs/5.5/routing

相关内容

最新更新