Serve multiple pages from 1 PHP file?

前端 未结 5 1257
失恋的感觉
失恋的感觉 2021-01-19 15:23

So basically i am struggling with the need of optimizing my code for some project of mine. Currently i have pages like add_company.php, view_company.php

相关标签:
5条回答
  • 2021-01-19 15:56

    This is pretty simple stuff. It should look something like this:

    //index.php
    if (!isset($_GET['page']))
        {
        require('index_contents.php');
        }
    else if ($_GET['page'] == 'company')
        {
        if (!isset($_GET['action']))
            {
            require('company_contents.php');
            }
        else if ($_GET['action'] == 'edit')
            {
            require('edit_company.php');
            }
        else if ($_GET['action'] == 'add')
            {
            require('add_company.php');
            }
        else
            {
            require('company_contents.php');
            }
        }
    else
        {
        require('index_contents.php');
        }
    
    0 讨论(0)
  • 2021-01-19 15:56

    You could use POST instead of GET with index.php:

    <?php
    
    require($_POST['action'] . ".php");
    
    ?>
    

    This would hide the action type from the user, acting as though it is a single page. However, it may require using a form in your navigation as opposed to linking direct to "company.php?action=edit".

    0 讨论(0)
  • 2021-01-19 15:59

    I always prefer using a switch statement for the $_GET variable. Its my personal preference to put all the logic pertaining to one entity (in this case company) in a single PHP file because I generally deal with tons of entities. If you want a MVC model, this might not be what you are looking for. Just my 2 cents.

    // Common page header
    // Other stuff common in the pages
    
    $page_to_load = $_GET[view];
    
    switch($page_to_load) {
    
    case 'view':
    //Logic to view or HTML for view
    break;
    
    case 'add':
    //Logic to add or HTML for add
    break;
    
    }
    // Common footer etc..
    
    0 讨论(0)
  • 2021-01-19 16:04

    A very easy solution would be to just use includes.

    if ($_GET["action"] == "view") {
      require("action_view.php");
    } else if ...
    

    (of course, you might want to use a switch statement, if you have lots of different page types)

    Then action_view.php contains just the code for the specific page.

    Better, but also more complicated solutions, would be an object oriented approach (abstract Page class with Factory Pattern) or forthright a good framework and template engine.

    0 讨论(0)
  • 2021-01-19 16:16

    You could make company.php

    <?php
    
    $allowed = array('add', 'view', 'edit');
    
    if ( ! isset($_GET['action'])) {
       die('missing param');
    }
    
    $action = $_GET['action'];
    
    if ( ! in_array($action, $allowed)) {
       die('invalid');
    }
    
    
    
    require $action . '_' . __FILE__;
    

    Quick and dirty, but should work :) You could place this code in any file, and it will work straight away.

    With a bit of modification, you could make this your front controller with index.php.

    0 讨论(0)
提交回复
热议问题