January 3, 2012

How to get Controller, Action, URL informations with CodeIgniter

Question by noname.cs

I have these urls:

How to get controller name, action name from these urls. I’m codeigniter newbie. Are there any helper function to get this info

Ex:

$params = helper_function( current_url() )

Where $params becomes something like

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

Answer by Jonathan Sampson

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

Answer by Starx

As an addition

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

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!