How to get current controller and method? |
[eluser]anna16[/eluser]
Hi I want to catch the current controller and method. Let say i have this url below, http://www.mysite.com/ci/index.php/site/meter/ now what i want is to get these last two partial url --> /site/meter/ How to do this? Thanks in advanced.
[eluser]Bas Vermeulen[/eluser]
Hi Anna, To get the controller: Code: $this->router->class To get the method: Code: $this->router->method
[eluser]7amza[/eluser]
for controller : Code: $this->uri->segment('1'); Code: $this->uri->segment('2');
[eluser]Sbioko[/eluser]
These all are wrong. Correct solution: Code: $this->router->fetch_class(); //controller
[eluser]Bas Vermeulen[/eluser]
Sbioko, why is my solution wrong? It's how I use it in my applications, works perfectly...
[eluser]Sbioko[/eluser]
Bas Vermeulen, because you are getting object's values directly. Maybe in CI 2 the way CI stores this data will change and you method will fail. In all places of CI core where core CI libraries get controller and method names, they use those methods(get_class & get_method).
[eluser]Bas Vermeulen[/eluser]
Hmmm, I got your point so went to try your solution but that didn't work for me. The get_class and get_method methods didn't return any data for me whilst the class and method methods did. I use the most recent CI 2 version from BitBucket.
[eluser]Sbioko[/eluser]
Oops, sorry! Updated code: Code: $this->router->fetch_class();
[eluser]Sbioko[/eluser]
Bas Vermeulen, you're welcome :-) Always glad to help! You can contact me any minute by credentials I wrote in my signature. |
Welcome Guest, Not a member yet? Register Sign In |