【问题标题】:codeigniter 3 URI routing - geting right hand side from given urlcodeigniter 3 URI 路由 - 从给定的 url 获取右侧
【发布时间】:2020-05-30 07:52:58
【问题描述】:

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

例如,我有一些路线:

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

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

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

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

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

更新: 我正在查看整个系统/路由器类,我发现受保护的函数 _parse_routes 正在做类似的事情,所以如果没有可以给我所需的函数,我将基于这个创建一个。

【问题讨论】:

标签: php codeigniter codeigniter-3


【解决方案1】:

使用这个

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

你会得到

Main/blog/en

【讨论】:

  • 就这么简单? $this->router->routes['blog/petar'] 也能正常工作吗?
  • @DejanDozet 您可以使用$this->router->routes['blog/(:any)'] 检索将返回Main/blog/en/$1的内容
  • @DejanDozet 你也可以使用uri_string(); 来获取整个URL 字符串
【解决方案2】:

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

又名:该控制器/语言参数/博客文章中的控制器类/函数

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-11
      • 1970-01-01
      • 2014-08-26
      • 2014-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多