繁体   English   中英

动态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 ROUTER:

基本逻辑是,您正在将url和具有其方法(post,get)的“对象”映射到哪个对象,哪个控制器将处理该对象以及什么是控制器方法。

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

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

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

并在您的index.php文件中

您检查用户输入的网址是否在您映射的$ 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();
    }





}

您只需利用“面向对象编程”的优势。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM