CodeIgniter Forums
Is there a way to get the controller name inside a view? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Is there a way to get the controller name inside a view? (/showthread.php?tid=19611)



Is there a way to get the controller name inside a view? - El Forum - 06-12-2009

[eluser]jstuardo[/eluser]
I have created a PHP that contains common functions I have to use in a control panel I am programming. One function is called display_toolbar($title).

That function displays the title of the current page and a toolbar with some buttons, for example, "Create" or "Delete".

The anchor for those operations is different depending the controller. For example, if the toolbar is shown in the users list page, "Create" operation has to be:

Code:
anchor('usuario/create', '<img src="img.jpg" alt="Crear" />')

But for the list shown in events list page, "Create" operation has to be:

Code:
anchor('evento/create', '<img src="img.jpg" alt="Crear" />')

Is there a way to get the controller segment dinamically depending which controller was loaded?

Thanks in advance
Jaime


Is there a way to get the controller name inside a view? - El Forum - 06-12-2009

[eluser]Dam1an[/eluser]
you can get the name of the controller using
Code:
$this->router->class;



Is there a way to get the controller name inside a view? - El Forum - 06-12-2009

[eluser]SpooF[/eluser]
Code:
get_class($this)

Will return the name of the class that $this belongs too. Only problem is that will return the first class the controller extends.

Code:
&lt;?php
class A {
    function foo() {
      return get_class();
    }
}
class B extends A {
   function bar() {
      return get_class();
   }
}
$instance = new B();
echo $instance->bar(); //Prints 'B';
echo $instance->foo(); //Prints 'A';
?&gt;



Is there a way to get the controller name inside a view? - El Forum - 06-12-2009

[eluser]TheFuzzy0ne[/eluser]
Code:
$this->uri->rsegment(1);

Does pretty much the same thing too. Ooh, aren't you just spoilt for choice. Tongue


Is there a way to get the controller name inside a view? - El Forum - 06-12-2009

[eluser]jstuardo[/eluser]
Thanks! it worked.

Jaime

[quote author="Dam1an" date="1244847379"]you can get the name of the controller using
Code:
$this->router->class;
[/quote]