CodeIgniter: How to get Controller, Action, URL information

前端 未结 11 1195
死守一世寂寞
死守一世寂寞 2020-12-07 09:21

I have these URLs:

  • http://backend.domain.com/system/setting/edit/12
  • http://backend.domain.com/product/edit/1

How to get controller nam

相关标签:
11条回答
  • 2020-12-07 10:11

    You could use the URI Class:

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

    I've also been told that the following work, but am currently unable to test:

    $this->router->fetch_class();
    $this->router->fetch_method();
    
    0 讨论(0)
  • 2020-12-07 10:14

    Update

    The answer was added was in 2015 and the following methods are deprecated now

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

    Hi you should use the following approach

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

    for this purpose but for using this you need to extend your hook from the CI_Controller and it works like a charm, you should not use uri segments

    0 讨论(0)
  • 2020-12-07 10:15

    Instead of using URI segments you should do this:

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

    That way you know you are always using the correct values even if you are behind a routed URL, in a sub-domain, etc.

    0 讨论(0)
  • 2020-12-07 10:16

    Use This Code anywhere in class or libraries

        $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
    
    0 讨论(0)
  • 2020-12-07 10:21

    As an addition

    $this -> router -> fetch_module(); //Module Name if you are using HMVC Component
    
    0 讨论(0)
提交回复
热议问题