Autoload a class? |
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/l...oader.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
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.
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. What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
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.
CI 4 uses the PSR-4 autoload. You just need to setup your paths to your libs.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
|
Welcome Guest, Not a member yet? Register Sign In |