php - 如何获取URI的特定部分?

标签 php wrapper

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

假设我有一个如下所示的 URI:localhost/api/user/method/5
URI 由以下部分组成:

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

我想要做的是创建 user.php 文件 Controller 中可用的类 User 的实例,调用函数 getUseruser Controller 中可用并传递参数 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

现在我创建了一个类,它是一个简单的路由器系统。

<?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并将其保存为 Controller ,[3]索引应该用作在 Controller 上指示的功能,[4]索引应该是参数,注意:[4]索引可以是可选参数。实际上该类的工作原理如下:

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

$route->submit();

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

localhost/api/user/5

将打印:

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

关于php - 如何获取URI的特定部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35412886/

相关文章:

javascript - 如何实现这段php代码

function - 在 Go 中将函数作为参数传递

跟踪变化的 Javascript 对象封装

php - 如何将 MySQL 中数字列单元格的值减一?

PHP 将查询字符串传递到另一个页面

php - 显示每个卧室数量的图标(酒店网站)php

php - Request->file 返回 null 为什么?

c - 包含引用本地 C 库的本地 Perl 模块

c++ - 获取对 function_name 的 undefined reference

c# - PInvoke 签名与非托管目标签名不匹配