如何获取URI的特定部分?

时间:2016-02-15 15:16:54

标签: php wrapper

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

假设我有这样的URI:localhost/api/user/method/5
URI由以下内容组成:

[0] => localhost: 服务器基本路径
[1] => api: 应用程序基本路径
[2] => 用户: 代表用户控制器
[3] => getUser: 代表方法getUser
[4] => 5: 是参数

我想要做的是创建User文件控制器中可用的类user.php的实例,调用getUser控制器中可用的函数user并传递参数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应该用作在控制器上指示的函数,以及{{1应该是参数,请注意:[4] index可以是可选参数。实际上班级简单的工作如下:

[4] index

所以如果用户在浏览器中插入此URI:

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

$route->submit();

将打印出来:

  

姓名5

我如何实现上述逻辑?

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

localhost/api/user/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

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

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

那么如何将所有这些参数传递给函数localhost/api/user/method/5/9

1 个答案:

答案 0 :(得分:2)

$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']); 
}