CodeIgniter Forums
using other controller's method - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: using other controller's method (/showthread.php?tid=29716)



using other controller's method - El Forum - 04-19-2010

[eluser]thevenin[/eluser]
Hi guys,

I have two controllers: one for admin panel and one for frontend app.
In both places I need a method which will output an array of objects - let's say the method name is "Rankings".

Admin panel controller:
Code:
class Standings extends Controller {

  ....

  function Rankings() {
    //some code
  }

  ....
}

I would like to call this method in my front controller Frontpage:

Code:
class Frontpage extends Controller {

   //is it possible to call it somehow here?
   //or I have to paste the whole method from Standings controller?

}



using other controller's method - El Forum - 04-19-2010

[eluser]Mareshal[/eluser]
Frontpage.php

Code:
require "Standings.php";

class Frontpage extends Controller {

   function test(){
        $stang = new Standings();
        $stand::Rankings(); //$stand->Rankings(); works too
   }

}

BUT, this is not recommended. try building rankings() function inside a model/library/helper . Is better.


using other controller's method - El Forum - 04-19-2010

[eluser]thevenin[/eluser]
Thanks.


using other controller's method - El Forum - 04-19-2010

[eluser]John_Betong[/eluser]
 
Search this forum for "extends MY_Controller".
 
 
 


using other controller's method - El Forum - 04-19-2010

[eluser]Phil Sturgeon[/eluser]
Directly calling controllers from other controllers is like bending MVC over a barrel and cracking out a truncheon... That is a horrible way to work and if you find yourself needing to do it then you are clearly doing something wrong.

Either share your code in a library so the logic can be used correctly in both controllers, or redirect users to the correct controller.