简体   繁体   English

动态php子页面

[英]Dynamic php subpages

I would like to know how to create a subpage via php. 我想知道如何通过php创建子页面。 I know there's a way by using the GET parameters, like for example: 我知道有一种使用GET参数的方法,例如:

example.com/index.php?category=1

I am more interested in the functionality of something found for example on instagram.com: 我对例如instagram.com上发现的某些功能更感兴趣:

instagram.com/example

How is the example below generated? 以下示例是如何生成的? How does this system work? 该系统如何工作?

I'd like to have a simple page showing the content according to the identifier after the dash. 我想要一个简单的页面,根据破折号后的标识符显示内容。 Also, how do they remove the .php extension on every professional site? 另外,他们如何在每个专业网站上删除.php扩展名?

Thanks in advance 提前致谢

this is typicall done using MVC frameworks such as laravel, codeigniter etc. There are many available with many different ways of achieving what you are looking for. 这通常是使用MVC框架(例如laravel,codeigniter等)完成的。有许多可用的方法来实现您所寻找的东西。

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

There are many advantages of using MVC's including adopting good structures to pages and may give you the functionality you are looking for in prebuilt packages 使用MVC有很多优点,包括采用良好的页面结构,并可能为您提供预构建包中正在寻找的功能。

I would suggest doing some research into a few such as laravel and see how you get on. 我建议对诸如laravel之类的产品进行一些研究,看看您的情况如何。

You can also change apache configurations like others have stated in the htaccess file. 您也可以更改htaccess文件中其他说明的apache配置。

What you are looking for is URL REWRITING . 您正在寻找的是URL REWRITING Depending on the HTTP server you are using, there are several ways to acomplish this. 根据所使用的HTTP server ,有几种方法可以完成此操作。

Most common used HTTP Server is Apache. 最常用的HTTP服务器是Apache。

Create a php file containing the following: 创建一个包含以下内容的php文件:

<?php
phpinfo();
?>

Open the page with your browser and you should be able to see what HTTP server you are running. 使用浏览器打开页面,您应该能够看到正在运行的HTTP服务器。 Search for SERVER_SOFTWARE which must say something like Apache , Nginx or LightHTTP . 搜索SERVER_SOFTWARE ,其中必须说类似ApacheNginxLightHTTP

If the server is using apache, you should be googling for apache php .htaccess url rewriting Other wise you could search for [server software] php url rewriting or [server software] php pretty urls 如果服务器使用的是apache,则应搜索apache php .htaccess url rewriting否则,您可以搜索[server software] php url rewriting[server software] php pretty urls

On the internet are tons of people that asked the same question before, so I figure you might be able to help yourself from here. 互联网上有成千上万的人问过同样的问题,所以我认为您也许可以从这里帮助自己。 Good luck! 祝好运!

It's done by the technic called URL routing there is some couple of ways..It's not that easy to figure out how exactly instagram doing this.. 这是由称为URL路由的技术完成的,有几种方法。弄清楚instagram是如何做到这一点并不容易。

There is a good example of non object oriented aaproach at: 有一个很好的非面向对象的例子:

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

Also most php frameworks(Laravel etc) provides that future.. 大多数PHP框架(Laravel等)也提供了未来。

Personally for now i'm using a php package called AltoRouter https://github.com/dannyvankooten/AltoRouter 就我个人而言,我现在使用一个名为AltoRouter的php包https://github.com/dannyvankooten/AltoRouter

And i guess there is plenty of more other ways.. 我想还有很多其他方法。

USING ALTO ROUTER: 使用ALTO ROUTER:

The basic logic is u are mapping the url an to an "object" with its methods(post,get) and which controller will handle that and what is the controller method.. 基本逻辑是,您正在将url和具有其方法(post,get)的“对象”映射到哪个对象,哪个控制器将处理该对象以及什么是控制器方法。

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

and there is an ExampleController class with an getShowExamplePage() method 还有一个带有getShowExamplePage()方法的ExampleController类

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

and in ur index.php file 并在您的index.php文件中

u check the url entered by user is in ur mapped $router object? 您检查用户输入的网址是否在您映射的$ 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();
    }





}

Simply u are taking the advantages of "object oriented programming". 您只需利用“面向对象编程”的优势。

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

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