Blizzard
Blizzard

Reputation: 65

Howto Setup Index.php dynamically?

I currently have this setup

<?php

$nav = filter_input(INPUT_GET, 'nav', FILTER_SANITIZE_STRING);

$Working_On = true;
//$Working_On = false;


if ($Working_On == true) {
    $title = '- Under Construction';
    $error_msg = "Website Under Construction.";
    include('404.php');
    exit;
} else {
    switch ($nav) {
        case "":
            include('main.php');
            break;
        default:
            $title = '- 404 Page Not Found';
            include('404.php');
            break;
    }
}

I'd love to know if there is a better way more efficent way of orginising this type of setup so i can easily add more options ?

Upvotes: 0

Views: 33

Answers (1)

Florian Schwalm
Florian Schwalm

Reputation: 166

While accessing a page with

example.com/?nav=examplepage

is a valid approach (that I also used in past projects), today's standard is to use something like

example.com/examplepage

This creates URLs that are easier to understand by humans and more semantic than query parameters. Doing so will also prevent you from running into possible caching issues with URLs defined by query parameters.

It also brings you the benefit of including even more information in the URL without the need for query parameters, for example

example.com/posts/view/20

Such URLs are usually parsed by a "Router". There are many reference and open source implementations for PHP routing on the web, just search for "PHP Router", take a look and decide what best fits your needs.

Side note:

  • use type-safe checks, aka === instead of == to save you a lot of headache and follow security best practices; if you're still at the beginning of your project, you might want to take a look at https://phptherightway.com/

  • you exit() if the site is on maintenance. In that case you don't need to put the switch statement inside else{}, you can just put it after the if clause

Upvotes: 1

Related Questions