two controllers doing 90% the same things

Permalink 1 user found helpful
Hello,

i have a page list controller and a block controller doing for a 90% the same things/calculations. The big difference is how i obtain the variables (page attributes). Is there a proper way to include a 'shared' file in both controllers so i can maintain the changes better.

thank you

Gr

Marco

stmarco
 
jvansanten replied on at Permalink Reply
I use a model, which can include one or more classes.

Define the class/classes as a model in file mymodel.php and place it in the models folder.

Helpers serve the same function, but are a smaller aggregate -- of functions.

Then, in your controller, place the following code which initiates
the model class as the class proper is being instantiated:

public function __construct () {
Loader::model("mymodel") ;
$this->newclass = new Mymodel ("desiredclass");
}

Probably a simpler approach for added functions would be an include file, and you could stick that in an appropriate place such as "tools" and simply include it.
stmarco replied on at Permalink Reply
stmarco
Thanks J

i will look into that, usually i just need a push in the right direction like you just did. I will take a look at both paths but i think for me the shared functions in the tools dir will be all i need.

gr

Marco
JohntheFish replied on at Permalink Reply
JohntheFish
Common code could be in a model, a library, a helper or an element.

If it is just a section of a form, an element may be most appropriate.

If it is a widget or anything that can work with a single global instance, a helper may be most appropriate.

If it is a conceptual model of data or something the code is manipulating, maybe a model is most appropriate.

A library could be used for anything, from a collection of static functions to a base class to be inherited by another library or model.

There is considerable overlap between the use of any of these. Mostly it is a matter of your preferred semantics. Though if a package was submitted to the PRB and an elemnt was used to directly access the database (rather than a model), you could be asked to justify doing so.
jvansanten replied on at Permalink Reply
John --

Thanks for your clarification here as to the proper use of these various approaches to library management.
stmarco replied on at Permalink Reply
stmarco
Thanks for The info John,

I still have a lot to learn :)

Gr.

Marco