CodeIgniter Forums
shared methods? - 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: shared methods? (/showthread.php?tid=36602)



shared methods? - El Forum - 12-08-2010

[eluser]nofx[/eluser]
I was thinking about the possibilities with this framework and i have a question that i could't find out myself.

Suppose i have a view like this:
Code:
<html>
<head></head>
<body>
<?= $menu ?>
<?= $content ?>
</body>
</html>

When i call for exmaple to following url: site.com/blog
Then my blogController is used. In that controller i have a method called 'loadMenu()', which loads a menu in $menu.

But when i go the another controller, for instance: site.com/news
Then that means i have to copy the method 'loadMenu()' from my blogController to my newsController inorder to show the menu again. But that means duplicate code which is not good...

How can this problem be solved??


shared methods? - El Forum - 12-08-2010

[eluser]Bart Mebane[/eluser]
You can extend the base Controller by putting MY_Controller.php (extends Controller) in your libraries folder. CI will load it automatically. Your other controllers would then extend MY_Controller, which would contain any common methods that you need.


shared methods? - El Forum - 12-08-2010

[eluser]nofx[/eluser]
[quote author="Bart Mebane" date="1291832611"]You can extend the base Controller by putting MY_Controller.php (extends Controller) in your libraries folder. CI will load it automatically. Your other controllers would then extend MY_Controller, which would contain any common methods that you need.[/quote]

I see, so instead of extending the real base controller, i just make my own base controller (lets say 'sharedController.php') which then extends the original base controller, right?

That could be a good possibility,


shared methods? - El Forum - 12-08-2010

[eluser]Bart Mebane[/eluser]
[quote author="nofx" date="1291836773"]
I see, so instead of extending the real base controller, i just make my own base controller (lets say 'sharedController.php') which then extends the original base controller, right?
[/quote]
That's right. But if you want CI to load it automatically, you need to name it MY_Controller (you can change the prefix in config.php). Otherwise you would need to write your own include or require statement to load it before you extend it, or write an __autoload function to load it. See Creating Core Classes (Extending Core Classes section) in the User Guide.


shared methods? - El Forum - 12-08-2010

[eluser]pickupman[/eluser]
This maybe more helpful. In application/libraries/MY_Controller.php for 1.7.x
Code:
class MY_Controller extends Controller{

public function __construct(){
    parent::__construct();
}

private function loadMenu(){
    //Do your menu stuff here
}

}

Then in each of your other controllers use
Code:
class Blog extends MY_Controller{

  public function __construct(){
     parent::__construct();
  }

  public function some_method(){
     $data['menu'] = $this->loadMenu();
  }
}

Here's a great link and examples to use. [url="http://philsturgeon.co.uk/index.php/news/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY"]Phil Sturgeon's Base Controllers[/url]