如何获取 URI 的特定部分


How to get specific part of URI?

标题有点误导。让我用一个例子更好地解释:

假设我有一个这样的 URI:localhost/api/user/method/5 .
URI 由以下部分组成:

[0] => 本地主机:服务器基本路径
[1] => API:应用程序基本路径
[2] => 用户:表示用户控制器
[3] => getUser:表示方法getUser
[4] => 5:是一个参数

我想做的是创建一个user.php文件控制器中可用的类User的实例,调用user控制器中可用的函数getUser并将参数传递给5。所以应该是这样的(示例代码(:

$Class = ucfirst("user"); 
// create an instance
$ctr = new $Class();
// and call the requested function
$ctr->$func();  //$func should be valorized with getUser in this case

现在,我已经创建了一个简单的router系统的类。

<?php
    class Route
    {
        /**
         * @var array $_listUri List of URI's to match against
         */
        private $_listUri = array();
        /**
        * @var array $_listCall List of closures to call 
        */
       private $_listCall = array();
       /**
       * @var string $_trim Class-wide items to clean
       */
       private $_trim = '/'^$';
       /**
       * add - Adds a URI and Function to the two lists
       *
       * @param string $uri A path such as about/system
       * @param object $function An anonymous function
       */
       public function add($uri, $function)
       {
          $uri = trim($uri, $this->_trim);
          $this->_listUri[] = $uri;
          $this->_listCall[] = $function;
       }
       // submit - Looks for a match for the URI and runs the related function
       public function submit()
       {    
           $uri = isset($_REQUEST['uri']) ? $_REQUEST['uri'] : '/';
           $uri = trim($uri, $this->_trim);
          $replacementValues = array();
          //  List through the stored URI's
         foreach ($this->_listUri as $listKey => $listUri)
         {
             // See if there is a match
              if (preg_match("#^$listUri$#", $uri))
              {
                 //Replace the values
                 $realUri = explode('/', $uri);
                 $fakeUri = explode('/', $listUri);
                // Gather the .+ values with the real values in the URI
                foreach ($fakeUri as $key => $value) 
                {
                    if ($value == '.+') 
                    {
                        $replacementValues[] = $realUri[$key];
                    }
                }
               // Pass an array for arguments
               call_user_func_array($this->_listCall[$listKey], $replacementValues);
        }   
      } 
   }
}

我想要实现的是将基本 url 路径作为[2] => user并将其另存为控制器,[3] index应该用作控制器上指示的功能,[4] index应该是参数,请注意:[4] index可以是可选参数。实际上该类的工作原理很简单:

$route->add('/user/.+', function($name) {
    echo "Name $name";
});
$route->submit();

因此,如果用户在浏览器中插入此 URI:

localhost/api/user/5

将被打印:

名称 5

如何实现上述逻辑?

更新 - 使用一些参数调用函数

$controller = $realUri[0]; 'Contains the controller to load
$func = $this->_listCall[$listKey]; 'contains the function to load
include dirname(dirname(__DIR__)) . "/application/controllers/" .
$controller . ".php" ;
$Class = ucfirst($controller);
$ctr = new $Class();
//$ctr->$func($replacementValues);

变量$replacementValues如下所示:

array(2) { [0]=> string(1) "5" [1]=> string(1) "9" }

并包含在 url 中传递的所有参数:

localhost/api/user/method/5/9

那么如何将所有这些参数传递给函数$func()呢?

$url        = "http://localhost:80/api/user/method/5";
$parsedUrl  = parse_url($url);
var_dump($parsedUrl);

将打印

array(4) {
  ["scheme"]=>
  string(4) "http"
  ["host"]=>
  string(9) "localhost"
  ["port"]=>
  int(80)
  ["path"]=>
  string(18) "/api/user/method/5"
}

现在,根据需要分解路径非常简单:

if (!empty($parsedUrl['path'])) {
    $patg = explode('/',$parsedUrl['path']); 
}