codeigniter 3 URI 路由 - 从给定 url 获取右侧

codeigniter 3 URI routing - geting right hand side from given url

我需要在 Codeigniter 3 中模拟路由,所以我的问题是如何以编程方式从任何 URL 获取右侧?

比如我有的一些路线:

$route["blog"] = "Main/blog/en";
$route["blog/(:any)"] = "Main/blog/en/";
$route["novosti"] = "Main/blog/sr";
$route["novosti/(:any)"] = "Main/blog/sr/";
$route["contact"] = "Main/contact/en";
$route["kontakt"] = "Main/contact/sr";

现在我需要一个函数,它可以 return 右侧给定的 URL 部分,如下所示:

echo $this->route->item("novosti/petar")

应该打印 Main/blog/sr/$1 或 Main/blog/sr/petar

Codeigniter 中有这样的功能吗,因为我在文档中找不到它?

更新: 我正在查看整个 system/router class 并且我看到受保护的函数 _parse_routes 正在做类似的事情所以如果没有可以给我我需要的功能我将基于这个创建一个.

使用这个

$this->router->routes['blog']

你会得到

Main/blog/en

Codeigniter 很简单,太简单了...而且因为对我来说那个函数在哪里并不明显(如果存在的话)我刚刚采用 _parse_routes 来解析 URL( slug) 到右侧,从中我可以更容易地找到相应的文件。

这里是(如果有人遇到和我一样的情况)。

  function parseRoute($uri) {

    // Get HTTP verb
    $http_verb = isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : 'cli';

    // Loop through the route array looking for wildcards
    foreach ($this->router->routes as $key => $val) {
      // Check if route format is using HTTP verbs
      if (is_array($val)) {
        $val = array_change_key_case($val, CASE_LOWER);
        if (isset($val[$http_verb])) {
          $val = $val[$http_verb];
        } else {
          continue;
        }
      }

      // Convert wildcards to RegEx
      $key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);

      // Does the RegEx match?
      if (preg_match('#^' . $key . '$#', $uri, $matches)) {
        // Are we using callbacks to process back-references?
        if (!is_string($val) && is_callable($val)) {
          // Remove the original string from the matches array.
          array_shift($matches);

          // Execute the callback using the values in matches as its parameters.
          $val = call_user_func_array($val, $matches);
        }
        // Are we using the default routing method for back-references?
        elseif (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE) {
          $val = preg_replace('#^' . $key . '$#', $val, $uri);
        }

        return $val;
      }
    }

    // If we got this far it means we didn't encounter a
    // matching route so we'll set the site default route
    return null;
  }

现在,这个:

echo parseRoute("novosti/petar")

将产生:

Main/blog/sr/petar

又名:控制器 class/控制器内的函数/语言参数/博客文章

您可以使用以下代码获取所需的信息。

$this->router->routes['novosti/(:any)'];