CodeIgniter: How to get Controller, Action, URL information

前端 未结 11 1194
死守一世寂寞
死守一世寂寞 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 09:57

    Last segment of URL will always be the action. Please get like this:

    $this->uri->segment('last_segment');
    
    0 讨论(0)
  • 2020-12-07 09:58

    Another way

    $this->router->class
    
    0 讨论(0)
  • 2020-12-07 09:58

    If you using $this->uri->segment , if urls rewriting rules change, segments name matching will be lost.

    0 讨论(0)
  • 2020-12-07 09:59
    $this->router->fetch_class(); 
    

    // fecth class the class in controller $this->router->fetch_method();

    // method

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

    controller class is not working any functions.

    so I recommend to you use the following scripts

    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];
    }
    
    0 讨论(0)
  • 2020-12-07 10:05

    The methods are deprecated.

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

    You can access the properties instead.

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

    See codeigniter user guide

    URI Routing methods fetch_directory(), fetch_class(), fetch_method()

    With properties CI_Router::$directory, CI_Router::$class and CI_Router::$method being public and their respective fetch_*() no longer doing anything else to just return the properties - it doesn’t make sense to keep them.

    Those are all internal, undocumented methods, but we’ve opted to deprecate them for now in order to maintain backwards-compatibility just in case. If some of you have utilized them, then you can now just access the properties instead:

    $this->router->directory;
    $this->router->class;
    $this->router->method;
    
    0 讨论(0)
提交回复
热议问题