CodeIgniter Forums

Full Version: using other controller's method
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

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

}

El Forum

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

El Forum

[eluser]thevenin[/eluser]
Thanks.

El Forum

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

El Forum

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