Skip to content
Advertisement

Is it possible to create sub links without creating a directory. using PHP

So I was wondering if it’s possible to create sub directories links as shows :

DOMAIN.TDL/users/USERNAME

I’m not looking for the URL variable solution, in other words :

DOMAIN.TDL/users?userid=int

Directory structure ( abstract ) :

 |/
├─ users/
│  ├─ users-template.php
├─ Index.php/

Let’s take YouTube as an example,

You see; when you go visit a YouTube Channel in URL you see :

youtube.COM/Channel/ChannelId

  • youtube.com – domain
  • /Channel/ – I believe it’s a directory
  • ChannelId – It cant be a directory ( well at least that what i think )

Also you can see it here on stack overflow

stackoverflow.com/questions/QuesId/QuesName

Well I do believe that this kind of pages runs on a template page that then fetches the data from a database to get the data associated with that page Id.

Well in

Domain/user?userid=int

you can request the userid integer/stringname number to fetch the data related to that number from the database.

How dose , YouTube or Stack over flow works with templates and getting the data without the URL variables.

Advertisement

Answer

A lot of websites use frameworks that handle this information on request. For example, Laravel, a framework for PHP will do this by using the router object. The object will parse the url and try to bind the information to the controller. There are many avenues to approach this but here is the laravel documentation to clarify url parsing.

https://laravel.com/docs/8.x/routing#route-parameters

If you would like to tackle this yourself and create a parser in php(assuming since this was tagged ‘php’) without the entire laravel framework, you can use the superglobal $_SERVER['REQUEST_URI'] and parse out each subdirectory then run it against your controller to attempt to get your expected request.

Also, note that these are usually not physical files, they are just variables that are parsed from the url similar to the $_GET variables that you mentioned above.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement