Which is safe, using required once or making one function in helper in codeigniter? Which is safe, using required once or making one function in helper in codeigniter? codeigniter codeigniter

Which is safe, using required once or making one function in helper in codeigniter?


I would say in general that calling a controller from within another controller is not a best practice. If two controllers need to use the same logic then maybe that logic should be in a separate place. This can be done by creating your own library and loading it within both controllers.

Check out the link below to learn more about creating libraries in CodeIgniter.

Creating Libraries


It is not so much a question of "safe" but more a matter of "convention" in the way things are done in the CodeIgniter (CI) framework. The "idea" behind a controller in CI is that on any given server request only one controller is created.

The reason using require to load another controller might not be "safe" is that you might introduce bugs that are very hard track down. There is a safer way to satisfy your needs.

In a situation where there is code that would be useful in more than one controller the best answer is probably to create a custom library that can be loaded and used were needed. A Helper might also be the answer. IMO, libraries (classes) are the OOP way and are prefered.

The CI documentation on Creating Libraries describes how to implement custom classes that are easily used in any number of controllers.