【问题标题】:CodeIgniter: How to get Controller, Action, URL informationCodeIgniter:如何获取Controller、Action、URL信息
【发布时间】:2011-01-04 22:44:06
【问题描述】:

我有这些网址:

如何从这些 URL 中获取控制器名称、动作名称。我是 CodeIgniter 新手。是否有任何帮助函数来获取此信息

例如:

$params = helper_function( current_url() )

$params 变成类似

array (
  'controller' => 'system/settings', 
  'action' => 'edit', 
  '...'=>'...'
)

【问题讨论】:

    标签: php codeigniter codeigniter-2


    【解决方案1】:

    你可以使用URI Class:

    $this->uri->segment(n); // n=1 for controller, n=2 for method, etc
    

    我还被告知以下工作,但目前无法测试:

    $this->router->fetch_class();
    $this->router->fetch_method();
    

    【讨论】:

    • 因为我的控制器有子文件夹,所以 $this->uri->segment(1) 返回控制器名称可能不正确。感谢您的帮助,我使用 $this->router 获取信息。
    • 为了正确获取目录,您还可以使用:$this->router->fetch_directory();
    • 嘿,如果您使用的是 Codeigniter 3,那么对于控制器使用:$this->router->class;对于方法:$this->router->method;对于目录:$this->router->directory;文档:codeigniter.com/user_guide/installation/…
    • $this->router->fetch_class(); $this->router->fetch_method();它有效。
    【解决方案2】:

    您应该这样做,而不是使用 URI 段:

    $this->router->fetch_class(); // class = controller
    $this->router->fetch_method();
    

    这样您就知道您始终使用正确的值,即使您位于路由 URL 后面、在子域中等等。

    【讨论】:

    • 我刚刚发现,使用这种方法,当调用像modules::run('module/controller/action') 这样的视图时,您无法获得正确的类方法名称。它只是显示加载页面的路由器信息。有什么办法可以克服吗?
    • 太棒了!关于这有多安全的任何消息? IE。这可以被欺骗吗?
    • 这应该是公认的答案 - 或使用快捷方式:$this->router->class
    • 如果您更改 routes.php 中的路由,$this->router 不会返回代码运行所在的类,但不会返回被覆盖覆盖的实际路由器。根据您想要做什么,这两个答案都非常有用。
    • 这些方法已被弃用,请改用$this->router->class$this->router->method
    【解决方案3】:

    这些方法已弃用。

    $this->router->fetch_class();
    $this->router->fetch_method();
    

    您可以改为访问属性。

    $this->router->class;
    $this->router->method;
    

    codeigniter user guide

    URI 路由方法 fetch_directory()、fetch_class()、fetch_method()

    具有属性CI_Router::$directoryCI_Router::$classCI_Router::$method 公开和他们各自的fetch_*() 没有 不再做任何其他事情来返回属性 - 它没有 保留它们是有意义的。

    这些都是内部的、未记录的方法,但我们选择了 暂时弃用它们以保持向后兼容性 以防万一。如果你们中的一些人已经使用了它们,那么你现在可以 改为访问属性:

    $this->router->directory;
    $this->router->class;
    $this->router->method;
    

    【讨论】:

      【解决方案4】:

      另一种方式

      $this->router->class
      

      【讨论】:

        【解决方案5】:

        作为补充

        $this -> router -> fetch_module(); //Module Name if you are using HMVC Component
        

        【讨论】:

        • 这个fetch_module() 调用在 CI 2.1.2 中对我不起作用。在 /system/router.php 文件中找不到。
        • @timpeterson,它仅在 HMVC 组件中可用,如答案中所述。
        【解决方案6】:

        更新

        答案是在 2015 年添加的,现在已弃用以下方法

        $this->router->fetch_class();  in favour of  $this->router->class; 
        $this->router->fetch_method(); in favour of  $this->router->method;
        

        您好,您应该使用以下方法

        $this->router->fetch_class(); // class = controller
        $this->router->fetch_method(); // action
        

        为此目的,但要使用它,您需要从 CI_Controller 扩展您的钩子,它就像一个魅力,您不应该使用 uri 段

        【讨论】:

          【解决方案7】:

          如果你使用 $this->uri->segment ,如果 url 重写规则改变,段名匹配会丢失。

          【讨论】:

            【解决方案8】:

            在类或库中的任何地方使用此代码

                $current_url =& get_instance(); //  get a reference to CodeIgniter
                $current_url->router->fetch_class(); // for Class name or controller
                $current_url->router->fetch_method(); // for method name
            

            【讨论】:

              【解决方案9】:

              URL 的最后一段始终是操作。请这样:

              $this->uri->segment('last_segment');
              

              【讨论】:

                【解决方案10】:
                $this->router->fetch_class(); 
                

                // fecth class 控制器中的类 $this->router->fetch_method();

                // 方法

                【讨论】:

                  【解决方案11】:

                  控制器类没有任何功能。

                  所以我建议你使用以下脚本

                  global $argv;
                  
                  if(is_array($argv)){
                      $action = $argv[1];
                      $method = $argv[2];
                  }else{
                      $request_uri = $_SERVER['REQUEST_URI'];
                      $pattern = "/.*?\/index\.php\/(.*?)\/(.*?)$/";
                      preg_match($pattern, $request_uri, $params);
                      $action = $params[1];
                      $method = $params[2];
                  }
                  

                  【讨论】:

                  • 有一些预定义的函数,例如 $this->router->fetch_class() 和 fetch_method() ,我们不需要手动操作。
                  猜你喜欢
                  • 2011-05-30
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2023-03-10
                  • 1970-01-01
                  • 2013-01-09
                  • 2016-12-22
                  相关资源
                  最近更新 更多