Welcome Guest, Not a member yet? Register   Sign In
How to get current controller and method?
#1

[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.
#2

[eluser]Bas Vermeulen[/eluser]
Hi Anna,

To get the controller:
Code:
$this->router->class

To get the method:
Code:
$this->router->method

Smile
#3

[eluser]7amza[/eluser]
for controller :
Code:
$this->uri->segment('1');
for method :
Code:
$this->uri->segment('2');
#4

[eluser]Sbioko[/eluser]
These all are wrong.
Correct solution:
Code:
$this->router->fetch_class(); //controller
$this->router->fetch_method(); //method
#5

[eluser]Bas Vermeulen[/eluser]
Sbioko, why is my solution wrong? It's how I use it in my applications, works perfectly...
#6

[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).
#7

[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.
#8

[eluser]Sbioko[/eluser]
Oops, sorry!
Updated code:
Code:
$this->router->fetch_class();
$this->router->fetch_method();
#9

[eluser]Bas Vermeulen[/eluser]
Ok, that works, updated my app, thanks!
#10

[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.




Theme © iAndrew 2016 - Forum software by © MyBB