动态php子页面

时间:2015-12-17 20:39:36

标签: php content-management-system

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

example.com/index.php?category=1

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

instagram.com/example

以下示例如何生成?这个系统如何运作?

我希望有一个简单的页面,根据短划线后的标识符显示内容。另外,他们如何删除每个专业网站上的.php扩展名?

提前致谢

3 个答案:

答案 0 :(得分:1)

这是典型的使用MVC框架完成的,例如laravel,codeigniter等。有许多可用的方法有许多不同的方法来实现你想要的。

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

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

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

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

答案 1 :(得分:1)

您要找的是URL REWRITING。根据您使用的HTTP server,有几种方法可以实现这一点。

最常用的HTTP Server是Apache。

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

<?php
phpinfo();
?>

使用浏览器打开页面,您应该能够看到正在运行的HTTP服务器。搜索SERVER_SOFTWARE,其必须说明ApacheNginxLightHTTP

如果服务器正在使用apache,您应该在Google上搜索apache php .htaccess url rewriting 另外,您可以搜索[server software] php url rewriting[server software] php pretty urls

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

答案 2 :(得分:1)

通过名为URL路由的技术完成了有几种方法。弄清楚这样做的确切方式并不容易。

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

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:

基本逻辑是你将网址映射到&#34;对象&#34;使用它的方法(post,get)以及哪个控制器将处理它以及控制器方法是什么..

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

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

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();
    }





}

您正在利用&#34;面向对象编程的优势&#34;。

相关问题