CodeIgniter Forums
[Solved] Autoloading core classes and composer - 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: [Solved] Autoloading core classes and composer (/showthread.php?tid=69150)



[Solved] Autoloading core classes and composer - enlivenapp - 10-13-2017

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.


RE: Autoloading core classes and composer - ChicagoPhil - 10-13-2017

Do you want to namespace your core controller files?


RE: Autoloading core classes and composer - enlivenapp - 10-13-2017

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...


RE: Autoloading core classes and composer - enlivenapp - 10-13-2017

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/archive/topic/234518/composer-breaks-exisiting-autoload-in-codeigniter

Notes: This works in CI 3.1.3 and 3.1.6


RE: [Solved] Autoloading core classes and composer - ChicagoPhil - 10-13-2017

I never seen that one before. Nice work. I uploaded a clean install to github with namespaces just in case you answered yes. LOL


RE: [Solved] Autoloading core classes and composer - enlivenapp - 10-13-2017

(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


RE: [Solved] Autoloading core classes and composer - Paradinight - 10-14-2017

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/no-more-my_controller-how-you-can-create-more-than-one-base-controller/


RE: [Solved] Autoloading core classes and composer - enlivenapp - 10-14-2017

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!