Location: PHPKode > projects > Breeze CMS - A PHP Content Manage System > libs/Ice/Http/Router.class.php
<?php
define("URI_DELIMITER", "/");
define("URI_KEY", "op");

class Ice_Http_Router
{
    function Ice_Http_Router()
    {
        
    }
    /**
     * get Request URI Router
     *
     * eg.
     * 1. /index.php?op=controller/action&var1=value1&var2=value2
     * 2. /index.php?/controller/action/var1/value1/var2/value2
     * 3. /index.php/controller/action/var1/value1/var2/value2
     * 4. /?/controller/action/var1/value1/var2/value2
     * 
     * @return array("controller" => controller,
     *               "action"     => action,
     *               "params"     => array("var1" => value1,
     *                                     "var2" => value2))
     */
    function getRouter()
    {
        $router = array();
        $path = $_SERVER['REQUEST_URI'];
        $request = & Ice_Http_Request::getGet();
        
        $strpos = strpos($path,'.php');
        if($strpos)
        {
            $path = substr($path,$strpos + 4);
            $path = trim($path, "?");
        }
        else
        {
            if(empty($_SERVER['QUERY_STRING']))
            {
                $strpos = strpos($path, '?');
                if($strpos)
                {
                    $path = substr($path,$strpos + 1);
                }
                else
                {
                    $path = '';
                }
            }
            else
            {
                $path = $_SERVER['QUERY_STRING'];
            }
        }

        // 第1.种形式,直接用$_GET数组获取
        if (strpos($path, URI_KEY))
        {
            $regs = explode(URI_DELIMITER, $request[URI_KEY]);
            $router['controller'] = $regs[0];
            $router['action'] = $regs[1];
            unset($request[URI_KEY]);
            
            $router['params'] = $request;
        }
        else
        {
            $path = trim($path, URI_DELIMITER);
            
            if (!empty($path))
            {
                $paths = explode(URI_DELIMITER, $path);
                
                $controller = preg_replace("/[^a-z0-9_]+/i", "", $paths[0]);
                $router['controller'] = !empty($controller) ? $controller : "index";
                
                $action = preg_replace("/[^a-z0-9_]+/i", "", $paths[1]);
                $router['action'] = !empty($action) ? $action : "index";
                
                $nums = count($paths);
                if ($nums > 2)
                {
                    for ($i = 2; $i < $nums; $i += 2)
                    {
                        $params[$paths[$i]] = (isset($paths[$i + 1])) ? $paths[$i+1] : '';
                    }
                }
                $router["params"] = $params;
            }
            else 
            {
                $router['controller'] = "index";
                $router['action'] = "index";
                $router['params'] = array();
            }
        }

        return $router;
    }
}
Return current item: Breeze CMS - A PHP Content Manage System