'Codeigniter 4 Current/Active Controller
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?
Solution 1:[1]
In CodeIgniter 4
$router = service('router');
$controller = $router->controllerName();
$router = service('router');
$method = $router->methodName();
Solution 2:[2]
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).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | mathan |
Solution 2 | Antony |