CodeIgniter Forums
Controller shared function - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Controller shared function (/showthread.php?tid=11850)

Pages: 1 2


Controller shared function - El Forum - 09-25-2008

[eluser]xinq88[/eluser]
Hello everyone,

I've been seeking for a good solution for my problem, but no luck so far.
So maybe you guys can help me out.

Here is the scenario:

I've got 2 controllers:

Code:
class Human extends Controller {

function work(){ $work->addWork(); }

}

class Work extends Controller {

function addWork() { // some stuff happens here }

}

So basically what I wanna achieve is that the class Human can interact with the class Work.
So I tried it with a library called 'work' and the Human Controller. But then I can't access the Work library by URL (like: http://localhost:8080/index.php/Work/addWork).

Conclusion:
I want access to functions of a controller IN an other controller.

edit:
Another question: How can I make more objects of a class in CI because when I declare a class in a library I can only load it once by using:

Code:
$this->load->library('name');

but what if I wanna do something like:

Code:
$eric = new Human("Eric", 24);
$john = new Human("John", 30);

$eric->age(); (returns: 24)
$john->age(); (returns: 30)

I hope my questions are clear to you guys, and I hope that somebody can help me.

Thanks to all of you


Controller shared function - El Forum - 09-25-2008

[eluser]sophistry[/eluser]
two thoughts:

1) use a model, or an extended controller that extends from a common base controller

2) if you use a model, you can assign it to an arbitrarily-named object.
http://ellislab.com/codeigniter/user-guide/libraries/loader.html


Controller shared function - El Forum - 09-25-2008

[eluser]Colin Williams[/eluser]
It really does depend on what the shared functionality is.

And for your second question, when you load a library, you've essentially included the file. Just because CI has instantiated it once doesn't mean you can't do it again:

Code:
$this->load->library('human');
$eric = new Human(array('name' => 'Eric', 'age' => '24'));



Controller shared function - El Forum - 09-25-2008

[eluser]xwero[/eluser]
I'm hacking the SVN code so much i don't even know what is in 1.6.4 anymore but in SVN the library method has a third parameter where you can define the object name.
Code:
$this->load->library('human',array('name' => 'John', 'age' => '30'),'john');



Controller shared function - El Forum - 09-25-2008

[eluser]Dready[/eluser]
Hello,

that's not poassible in Code Igniter to have a controller calling another controller. In your case I suggest you create a library (see doc here), a library can be loaded from any controller.


Controller shared function - El Forum - 09-25-2008

[eluser]xinq88[/eluser]
Aah right, so the second problem is not so hard to solve. Thats great!
Only the first problem is something I'd really like to find some solution for. As I said, a library (and a model)is not an option because you can't call that library with a URL as you can with a controller. like:

Code:
http://localhost/index.php/controller/function/parameter

Or is that possible?

Quote:or an extended controller that extends from a common base controller

That's neither an option because, what if I need to implement to controllers?
So a controller Human needs to implement a class/controller 'Work' and a class/controller 'Car'.
But when I type the url localhost/index.php/car/drive it has to got acces to the car class and the function drive().

I really don't know if this is possible at all, if it's not maybe the library solution is the best one in my case.

Thanks all for reacting and some great feedback!


Controller shared function - El Forum - 09-25-2008

[eluser]Rick Jolly[/eluser]
Colin's example is a good work-around, but be aware that CI will create an instance of the human class that will go unused. You could just include/require it instead.

Although it's nice to see that 1.6.4 no longer forces singleton libraries, it's a shame we're still using the loader with php 5. Compare this:
Code:
$this->load->library('human',array('name' => 'John', 'age' => '30'),'john');
To this:
Code:
$this->john = new library_human('John','30');
With php 5, __autoload(), and naming conventions the loader is obsolete.

If you want a singleton, you'd let the class offer it instead of that being under the loader's control:
Code:
$this->john = library_human::instance();

But I can appreciate that CI doesn't want to make exceptions for php 5 in order to keep things simple.


Controller shared function - El Forum - 09-25-2008

[eluser]xinq88[/eluser]
@Rick, thanks for your reaction, so the 'Colin solution' is the best way of doing it with CI?


Controller shared function - El Forum - 09-25-2008

[eluser]Rick Jolly[/eluser]
You could. The loader does 2 things (if the library hasn't already been instantiated):
1) includes the library
2) instantiates the class

So if you don't want a singleton library, why not just include it yourself?
Code:
include(APPPATH . 'libraries/human.php');



Controller shared function - El Forum - 09-25-2008

[eluser]xinq88[/eluser]
yeah that's also a good solution, but I'm looking for the best CI way. But it's a very good idea.
I don't see much negative sides on the 'colin way' though.

Maybe somebody a solution for my other scenario? (see post #5)