CodeIgniter Forums

Full Version: [Solved] Autoloading core classes and composer
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Well, the title is sorta right.

Here's what I'm trying to do:

Autoload classes in application/core.  IE: Admin_controller and Public_controller.  

I've done this in application/config with the following code (which works)

PHP Code:
function __autoload($class)
{
 if(
strpos($class'CI_') !== 0)
 {
 
 @include_once( APPPATH 'core/'$class '.php' );
 }



I'm also trying to use a composer package, (Stripe if it matters)

I've tried this in application/config.php (the normal place) and in the project root file index.php

PHP Code:
$config['composer_autoload'] =  'vendor/autoload.php'

Which does work but...

They don't work together at the same time.  If composer autoloading is 'on' the Admin_controller I get the error:
PHP Code:
Fatal error: Class 'Admin_Controller' not found in... 


When I turn composer autoloading back off, the Admin_controller is loaded and works correctly.  

I've tested this in PHP 5 and 7.1 with CI 3.1.3 - 3.1.6

So, my question is how do I get them both going at the same time?  Any help is appreciated.
Do you want to namespace your core controller files?
No, not for this project. I need to be able to update CI without that kind of a headache.

When we get to CI4 however...
Okies,

Seems I found the right answer here.  It's a little weird because I'm still not sure what the full behind the scenes are doing but here goes. Link at the bottom for a hard to find answer.

Place this code in application/config.php (I did it at the bottom)

PHP Code:
function __autoload($class)
{
 if(
strpos($class'CI_') !== 0)
 {
 
 @include_once( APPPATH 'core/'$class '.php' );
 }



Then in application/config.php change

PHP Code:
$config['composer_autoload'] =  false

to:

PHP Code:
$config['composer_autoload'] =  'vendor/autoload.php'// or wherever you've installed composer 


Then, in composer.json add:

PHP Code:
"autoload": {
 
         "classmap": ["application/core"]
     }, 


Lastly, in your terminal run:

PHP Code:
composer update 


Now I have access to Admin_controller when called and I have access to the Stripe (in my case) Object.



Reference:
https://expressionengine.com/forums/arch...odeigniter

Notes: This works in CI 3.1.3 and 3.1.6
I never seen that one before. Nice work. I uploaded a clean install to github with namespaces just in case you answered yes. LOL
(10-13-2017, 10:12 PM)ChicagoPhil Wrote: [ -> ]I never seen that one before. Nice work. I uploaded a clean install to github with namespaces just in case you answered yes. LOL

I appreciate it. This was perplexing for sure.

The good thing is I can charge people through my website now! Well, after a bunch more coding... lol
Quote:__autoload
Warnung
This feature has been DEPRECATED as of PHP 7.2.0. Relying on this feature is highly discouraged.


An other way:
http://avenir.ro/codeigniter-tutorials/n...ontroller/
Story of my life. lol

I should have known Avenir had already figured something out and written a blog post on it.

I'll check out the link. Thanks for posting it!