I have these URLs:
How to get controller nam
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();
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
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.
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
As an addition
$this -> router -> fetch_module(); //Module Name if you are using HMVC Component