124

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', 
  '...'=>'...'
)
Sofyan Thayf
  • 1,228
  • 2
  • 10
  • 22
noname.cs
  • 1,841
  • 4
  • 16
  • 20

11 Answers11

226

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();
Jano
  • 60,366
  • 20
  • 152
  • 174
Sampson
  • 251,934
  • 70
  • 517
  • 549
  • because my controller have sub folder, so $this->uri->segment(1) return controller name maybe incorrect. Thanks for your help, I use $this->router to get the informations. – noname.cs Jan 14 '10 at 04:32
  • 2
    For getting the Directory Correctly you can also use: $this->router->fetch_directory(); – jmserra Oct 08 '13 at 16:01
  • 6
    Hey if you are using Codeigniter 3, then for Controller use: $this->router->class; for Method: $this->router->method; for Directory: $this->router->directory; Documentation: http://www.codeigniter.com/user_guide/installation/upgrade_300.html#uri-routing-methods-fetch-directory-fetch-class-fetch-method – cartalot May 23 '16 at 21:55
  • $this->router->fetch_class(); $this->router->fetch_method(); it works. – Dinesh Patra Mar 17 '17 at 11:55
133

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.

Phil Sturgeon
  • 29,768
  • 12
  • 74
  • 117
  • I just found out, that using this method, you cannot get correct class method name when calling a view like `modules::run('module/controller/action')`. It just shows the router information of the loaded page. Is there any way to overcome this? – Starx Feb 13 '12 at 07:26
  • Awesome! Any word on how secure this is? I.e. can this be spoofed? – HellaMad Mar 13 '13 at 01:14
  • This should be the accepted answer - or using the shortcut: $this->router->class – ChristoKiwi Mar 15 '15 at 23:42
  • 1
    Not if you change the routes in routes.php, `$this->router` returns the class the code runs in, but not the actual router masked with the override. Both answers are quite useful based on what you want to do. – Tibor Szasz Jan 19 '16 at 11:08
  • These methods have been depricated, use `$this->router->class` and `$this->router->method` instead – Richard Jan 16 '17 at 07:14
30

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;
Community
  • 1
  • 1
lumos0815
  • 2,758
  • 2
  • 22
  • 23
12

Another way

$this->router->class
Luis Chanferoni
  • 336
  • 2
  • 4
11

As an addition

$this -> router -> fetch_module(); //Module Name if you are using HMVC Component
Starx
  • 72,283
  • 42
  • 174
  • 253
8

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

Reborn
  • 19,713
  • 8
  • 36
  • 61
3

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

monsterm
  • 31
  • 1
3

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
Aslam Patel
  • 834
  • 6
  • 17
1

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

$this->uri->segment('last_segment');
PrakashG
  • 1,625
  • 5
  • 18
  • 27
-1
$this->router->fetch_class(); 

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

// method

Fel
  • 164
  • 1
  • 10
-4

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];
}
Pang
  • 8,605
  • 144
  • 77
  • 113
  • There are predefined functions available like $this->router->fetch_class() and fetch_method() then we don't need to do manually. – Naveed Ramzan Mar 16 '17 at 19:59