PHP switch with GET request

后端 未结 6 1988
南旧
南旧 2021-02-03 16:12

I am building a simple admin area for my site and I want the URLs to look somewhat like this:

http://mysite.com/admin/?home
http://mysite.com/admin/?settings
htt         


        
相关标签:
6条回答
  • 2021-02-03 16:29

    As well as the ones mentioned, another option would be key($_GET), which would return the first key of the $_GET array which would mean it would work with URLs with other parameters

    www.example.com/?home&myvar = 1;

    The one issue is that you may want to use reset() on the array first if you have modified the array pointer as key returns the key of the element array pointer is currently pointing to.

    0 讨论(0)
  • 2021-02-03 16:35

    $_SERVER['QUERY_STRING']

    0 讨论(0)
  • 2021-02-03 16:36

    Use $_SERVER['QUERY_STRING'] – that contains the bits after the ?:

    switch($_SERVER['QUERY_STRING']) {
        case 'home':
            echo 'admin home';
            break;
    }
    

    You can take this method even further and have URLs like this:

    http://mysite.com/admin/?users/user/16/
    

    Just use explode() to split the query string into segments, get the first one and pass the rest as arguments for the method:

    $args = explode('/', rtrim($_SERVER['QUERY_STRING'], '/'));
    $method = array_shift($args);
    
    switch($method) {
        case 'users':
            $user_id = $args[2];
    
            doSomething($user_id);
            break;
    }
    

    This method is popular in many frameworks that employ the MVC pattern. An additional step to get rid of the ? altogether is to use mod_rewrite on Apache servers, but I think that's a bit out of scope for this question.

    0 讨论(0)
  • 2021-02-03 16:36

    The PHP code:

    switch($_GET){
        case !empty($_GET['home']):
           // code here
        break;
    
        case !empty($_GET['settings']):
             // code here
        break;   
    
        default:
             // code here
        break;
    }
    
    0 讨论(0)
  • 2021-02-03 16:41

    You can make your links "look nicer" by using the $_SERVER['REQUEST_URI'] variable.

    This would allow you to use URLs like:

    http://mysite.com/admin/home
    http://mysite.com/admin/settings
    http://mysite.com/admin/users
    

    The PHP code used:

    // get the script name (index.php)
    $doc_self = trim(end(explode('/', __FILE__)));
    
    /*
     * explode the uri segments from the url i.e.: 
     * http://mysite.com/admin/home 
     * yields:
     * $uri_segs[0] = admin
     * $uri_segs[1] = home
     */ 
    
    // this also lower cases the segments just incase the user puts /ADMIN/Users or something crazy
    $uri_segs = array_values(array_filter(explode('/', strtolower($_SERVER["REQUEST_URI"]))));
    if($uri_segs[0] === (String)$doc_self)
    {
        // remove script from uri (index.php)
        unset($uri_segs[0]);
    }
    $uri_segs = array_values($uri_segs);
    
    // $uri_segs[1] would give the segment after /admin/
    switch ($uri_segs[1]) {
        case 'settings':
            $page_name = 'settings';
            break;
        case 'users':
            $page_name = 'users';
            break;
        // use 'home' if selected or if an unexpected value is given
        case 'home':
        default: 
            $page_name = 'home';
            break;
    }
    
    0 讨论(0)
  • 2021-02-03 16:45

    Is not the most "elegant" way to do it but the simplest form to answer your question is..

    
        if (isset($_GET['home'])):  
            # show index..  
        elseif (isset($_GET['settings'])):  
            # settings...  
        elseif (isset($_GET['users'])):  
            # user actions..  
        else:  
            # default action or not...  
        endif;
    
    
    0 讨论(0)
提交回复
热议问题