3

In Codeigniter 3 is possible to get current active class and method with this code:

    $active_controller = $this->router->fetch_class();
    $active_function = $this->router->fetch_method();

Are there such functions in Codeigniter 4?

Tsefo
  • 409
  • 4
  • 15

3 Answers3

4

In CodeIgniter 4

$router = service('router'); 
$controller  = $router->controllerName();  

$router = service('router');
$method = $router->methodName();
mathan
  • 455
  • 3
  • 10
3

Its worth saying those classes were never officially part of CI3 (https://codeigniter.com/user_guide/installation/upgrade_300.html?highlight=fetch_class). Bearing in mind CI4 is a lot more flexible and that routes are defined more variably I would look at the routing side of things and extract it from there (https://codeigniter4.github.io/userguide/incoming/incomingrequest.html#the-request-url).

Antony
  • 3,875
  • 30
  • 32
  • It's not the get-right-away solution, but accepting it, since you have guided me to an answer to my problem. – Tsefo Jul 16 '19 at 11:28
  • 2
    Used uri_string(), to get what's current url and then used it to determine where exactly the user is. Now I got stuck with controller/method/parameters urls, but I find it I wouldn't need anything different, at least for now. Thank you. – Tsefo Jul 16 '19 at 11:32
1

You can use PHP constant or functions that provide you the same:

Get Current Function name:
__FUNCTION__
Get Current Class name:
__CLASS__

OR

get_class()

Codeigniter 4: All the above is working well otherwise use the same code that @mathan answered:

$router = service('router'); 
echo $router->controllerName();  
heySushil
  • 493
  • 7
  • 13