![]() |
Autoload a class? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Autoload a class? (/showthread.php?tid=72279) |
Autoload a class? - alext - 11-29-2018 Hi guys. I am new to codeigniter. I want to make my own App_Controller and extend all other controllers from this. I want to put it in controllers folder. But is not working. class Welcome extends App_Controller { is not finding it even if they are in the same contorller? There is no such thing as loading a file by its class name? I looked at https://www.codeigniter.com/user_guide/libraries/loader.html Looks like my only hope is to put my App_Controller in core and init that in my config or smth? I really think there should be an autoloader to look files by class name that is a big surprise for me not to find. Or is there a way? Cheers RE: Autoload a class? - Pertti - 11-29-2018 You are right, you have to put App_controller.php in application/core folder, and update application/config/config.php setting $config['subclass_prefix'] = 'App_'; Autoloading is a bit tricky. You don't want to scan through every potential folder and sub-folder, because that would make every request to your site very slow. Placing it in application/controllers folder also causes issue with auto-routing - you would now have www.mydomain.com/app_controller technically available, etc. I have't checked how CI4 handles this yet myself (which I really ought to), but that's how CI3 works, and it's not that unreasonable. RE: Autoload a class? - alext - 11-29-2018 thanks Pertii RE: Autoload a class? - InsiteFX - 11-29-2018 If you have more then one custom controller, you can use this spl autoloader for them. Just add it at the bottom of your ./application/config/config.php file. PHP Code: // ----------------------------------------------------------------------- Hope that helps. RE: Autoload a class? - dave friend - 11-29-2018 Here is a page - one part of a larger tutorial - that examines the multiple ways to add autoloading to CodeIgniter. I prefer the "hook" method myself. (Don't worry, that will make sense once you read the page.) I'm pretty sure the code for that approach is tucked away on the forum somewhere. RE: Autoload a class? - dave friend - 11-29-2018 (11-29-2018, 08:36 AM)Pertti Wrote: I have't checked how CI4 handles this yet myself (which I really ought to) Yes, you really should. (Spoiler Alert: namespaces and a PSR-4 compliant autoloader) RE: Autoload a class? - Pertti - 11-30-2018 (11-29-2018, 01:54 PM)dave friend Wrote: Yes, you really should. Tidying up my home office / warehouse over Christmas, and then I'll be on it ![]() RE: Autoload a class? - InsiteFX - 11-30-2018 CI 4 uses the PSR-4 autoload. You just need to setup your paths to your libs. |