简体   繁体   中英

Is it possible to call methods from one controller on another in Codeigniter?

If I am in a "signin" controller can I execute the method get_user_info() which resides in the "user" controller? If so, what would be the syntax for that?

I'm not saying this can't be done, however it really shouldn't be done.

Something like this is exactly what a library is meant for.

Create a library which handles functions such as get_user_info() and then load and call that library within whichever controllers need access to it.

You can still have a controller named user and a library named user as well (though that could get confusing).

Check out CI's guide for info on how to create a library .

* Edit *

As people have mentioned models could also serve the purpose of making functions usable in multiple places.

Personally I like to keep only business logic within models (the CRUD operations) and any logical operations which must be done in multiple places I create a library for.

have you looked at HMVC?
https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/wiki/Home

it will look something like modules::run('module/controller/method', $params, $...);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM