0

I don't how to go about it.

I have a single point of entry to my small site; presently the URL can only be accessed the old fashion way i.e

?action = anything

I need a PHP function on top on my index page to be able to things like:

 /register
 /{user}/edit
 /{product}

You get the idea.

This is my index code snippet:

switch($action)
{
    case '/':
        include('../app/views.php');
        break;

    case 'contact':
        include('../app/form.php');
        break;

    //auth start here
    case 'register':
        include('../app/register.php');
        break;
}

Thank you!

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
Young Code
  • 11
  • 8
  • 3
    That's not a simple task, so there's not a simple function to solve it. You need to write down the logic, then parse the url to find what page should be shown. My advice: don't reinvent the wheel. There are ton of frameworks or cms that will do this for you, ready, tested and safe. – Elias Soares Feb 13 '18 at 19:31
  • 3
    If you google "php routing packages" you will find examples of how others have done it if you want to learn, or you could even use one of those packages. – bumperbox Feb 13 '18 at 19:32
  • 1
    If you want your index.php file to also handle paths like "/register" and "/something" instead (i.e. you don't want to have "index.php" in the path), you need to configure your webserver correspondingly. This is not something PHP can do by itself, as the requests to the "clean paths" don't even get to the php script. – Tobias Xy Feb 13 '18 at 19:34
  • [Building a Simple Web API with Vanilla PHP](https://gist.github.com/odan/e791bb29e2a1eab4ac6f2be4bbf86e12) – odan Feb 13 '18 at 19:49
  • Thank you DanielO – Young Code Feb 13 '18 at 20:21

0 Answers0