简体   繁体   中英

What is the best way to handle page switching?

Currently I use switch($_GET['page']) to put it simply. This is a simple solution and works basically anywhere.

However, sadly some of the projects have grown so much, that I'm wondering, if there is a better..faster method for this?

This is my base for, how I switch pages currently:

// There is more complex .htacces to translation of friendly-urls behind it, but for example sake, these variables are being produced:
$lext = array(
    'parent_page' => 'about-us',
    'child_page' => 'map-to-somewhere',
    'child_id' => NULL, // if it would be a article or something, it would be example.com/{parent_page}/{child_id}-some-friendly-url.html
);

switch ($lext['parent_page']) {
    case 'about-us':
        // about us page
    break;
    case '':
        // home
    break;
    default:
        // 404
    break;
}

Inside the switch cases, it either triggers a function or includes a file. As it turned out to produce fastest page load results.

So I'm wondering, for a large amount of traffic and your "index.php" aka. the landing file getting alot of hits. What would be the fastest and most simplest solution?
As the simplest or stupidest solutions seem to produce the best results, I woulnt be suprised if:

if ($lext['parent_page'] == 'about-us') {
    // about us page
} else if ($lext['parent_page'] == '') {
    // home
} else {
    // 404
}

..would be faster and better beforming then switch() .

I have already searched SO for similar questions and tested all the answers, but the ones I have found, do not perform better.

Multiple answers to this. Depends largely on your project and how much refactoring you want to do. My concern would not be speed so much as code scalability and ease of maintenance. A switch with anything but a LOT of cases probably isn't going to cause you any noticeable slow-down vs. if-else or other means.

One approach might be to move into the world of MVC frameworks, which typically have a controller method per page, allowing you a nice, clean split in your code. For example, with Code Igniter you specify your pages thusly:

class MySite {

    /* constructor etc */

    public function page_1() {
        //this is called on visits to /page_1
        //load a view for this page, etc
    }

    public function page_13() {
        //this is called on visits to /page_3
        //load a view for this page, etc
    }

}

A simpler approach might be to make a JSON data file of the available cases and what should happen in each one.

{
    "page_1": {"inc": "page_1.php"},
    "page_13": {"func:": "some_func"},
}

Then, in your PHP:

//get data
$data = @file_get_contents($fp = 'pages_data.txt') or die("Couldn't load ".$fp);
$data = json_decode($data, 1);

//look for requested page in data - if found, include file or run function...
if (isset($data[$lext['parent_page']])) {

    $item = $data[$lext['parent_page']];

    //...include file
    if (isset($item['inc']) && file_exists($item['inc']))
        include $item['inc'];

    //...run function
    else if (isset($item'func']) && function_exists($item['func']))
        $item['func']();

} else
    //404...
}

Depends on how you manage your pages. If you have to require every page file, than you always can load that file by simply:

$page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 'index';

if (file_exists(__DIR__.'/views/'.$page.'.php')) {
    require(__DIR__.'/views/'.$page.'.php');
} else {
   switch ($page) {
       // Custom rules that does not fit to previous rule.
   }
}

I suggest using class / action structure to dynamically load requested page (like most frameworks).

[index.php]

$route = isset($_REQUEST['route']) ? $_REQUEST['route'] : 'index';
$page = explode('/', $route);

require_once(__DIR__.'/controller/'.ucfirst($route[0]).'Controller.php');

$className = ucfirst($route[0]).'Controller';
$class = new $className();
$class->{$route[1]}();

SOME WARNINGS

Always try to whitelist your requests, don't forget empty values defaults, use $_REQUEST if you may pass route information via POST or GET.


For SEO Url, you will be using .htaccess and database.

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