动态 php 个子页面

Dynamic php subpages

我想知道如何通过 php 创建子页面。我知道有一种方法可以使用 GET 参数,例如:

example.com/index.php?category=1

我对在 instagram.com 上找到的东西的功能更感兴趣:

instagram.com/example

下面的例子是如何生成的?这个系统是如何工作的?

我想要一个根据破折号后的标识符显示内容的简单页面。另外,他们如何删除每个专业网站上的 .php 扩展名?

提前致谢

这通常是使用 MVC 框架完成的,例如 laravel、codeigniter 等。有许多可用的许多不同的方法来实现您正在寻找的东西。

http://www.codeproject.com/Articles/826642/Why-to-use-Framework-in-PHP 列出了其中的一些。

使用 MVC 有很多优点,包括对页面采用良好的结构,并且可以为您提供您在预构建包中寻找的功能

我建议对 laravel 等一些研究进行一些研究,看看你的进展如何。

您也可以像其他人在 htaccess 文件中声明的那样更改 apache 配置。

您要找的是URL REWRITING。根据您使用的 HTTP server,有多种方法可以完成此操作。

最常用的 HTTP 服务器是 Apache。

创建一个包含以下内容的 php 文件:

<?php
phpinfo();
?>

用你的浏览器打开页面,你应该能看到你是什么HTTP服务器运行。搜索 SERVER_SOFTWARE,其中必须包含类似 ApacheNginxLightHTTP.

的内容

如果服务器使用的是 Apache,您应该使用谷歌搜索 apache php .htaccess url rewriting 否则你可以搜索 [server software] php url rewriting[server software] php pretty urls

之前在互联网上有很多人问过同样的问题,所以我想你可以从这里得到帮助。祝你好运!

它是由称为 URL 路由的技术完成的,有几种方法..要弄清楚 instagram 究竟是如何做到这一点并不容易..

有一个很好的非面向对象方法的例子:

http://blogs.shephertz.com/2014/05/21/how-to-implement-url-routing-in-php/

大多数 php 框架(Laravel 等)也提供了未来..

我个人目前正在使用一个名为 AltoRouter 的 php 包 https://github.com/dannyvankooten/AltoRouter

而且我想还有很多其他方法..

使用 ALTO 路由器:

基本逻辑是你正在将 url 映射到一个 "object" 及其方法(post,get),哪个控制器将处理它,控制器方法是什么..

$router->map('GET','/example', 'Controllers\ExampleController@getShowExamplePage' ,'example' );

还有一个带有 getShowExamplePage() 方法的 ExampleController class

public function getShowExamplePage(){
    include(__DIR__ . "/../../views/example.php");

在你的 index.php 文件中

您检查用户输入的 url 是否在您映射的 $router 对象中?

$match = $router->match();//it returns true or false

if(!match)
{
   //--
          u can redirect a error 404 PAGE
   //---
}

else

{ 
//Example the use entered url www.example.com/example

list($controller,$method) = explode("@",$match['target']);//To get what is the controller and its method.
    //If  that method of the contoller avaliable run that method
    if(is_callable(array($controller,$method))){
      $object = new $controller();



      call_user_func_array(array($object ,$method) , array($match['params']));

    }else {
      echo "Cannot find $controller-> $method";
      exit();
    }





}

只是你在利用 "object oriented programming"。