简体   繁体   中英

Dynamic php subpages

I would like to know how to create a subpage via php. I know there's a way by using the GET parameters, like for example:

example.com/index.php?category=1

I am more interested in the functionality of something found for example on 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?

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.

http://www.codeproject.com/Articles/826642/Why-to-use-Framework-in-PHP lists a few of these.

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

I would suggest doing some research into a few such as laravel and see how you get on.

You can also change apache configurations like others have stated in the htaccess file.

What you are looking for is URL REWRITING . Depending on the HTTP server you are using, there are several ways to acomplish this.

Most common used HTTP Server is Apache.

Create a php file containing the following:

<?php
phpinfo();
?>

Open the page with your browser and you should be able to see what HTTP server you are running. Search for SERVER_SOFTWARE which must say something like Apache , Nginx or LightHTTP .

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

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..

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/

Also most php frameworks(Laravel etc) provides that future..

Personally for now i'm using a php package called AltoRouter https://github.com/dannyvankooten/AltoRouter

And i guess there is plenty of more other ways..

USING 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..

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

and there is an ExampleController class with an getShowExamplePage() method

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

and in ur index.php file

u check the url entered by user is in ur mapped $router object?

$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".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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