Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter: How to get Controller, Action, URL information

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();

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.


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;

Another way

$this->router->class

As an addition

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