What is safe if you need to do it once or do a single function in the helper in the encoder?

I had one problem when calling a controller from another controller, one of the stackoverflow users suggested using require_once, but another person said it was not a good idea, now I'm confused what is safe? Using require_once or writing a function in a helper?

Old question link

+3


source to share


2 answers


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

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



Creating Libraries

+5


source


It is not so much about "secure" as "agreement", as is done in CodeIgniter (CI). The "idea" of a controller in CI is that for any server request, only one controller is created.

The reason for using a require

different controller to load can be insecure is that you can introduce errors that are very difficult to track down. There is a safer way to meet your needs.



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

The CI documentation on Building Libraries describes how to implement custom classes that are easy to use across any number of controllers.

+2


source







All Articles