Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Using composer packages.
  • Composer is fantastic, and i've already pinpointed a few packages i want to use in a FuelPHP project, but i can't seem to get the autoloading working correctly, i was hoping someone could help clear things up for me? The package i'm wanting to use is Carbon:
    https://github.com/briannesbitt/Carbon I've installed composer.phar at fuel/app/composer.phar, and also have a composer.json file requiring the package. It downloads it successfully and installs it in fuel/app/vendor/nesbot/carbon, with the actual class being at the location: fuel/app/vendor/nesbot/carbon/Carbon/Carbon.php. I've tried adding lines similar to this to my fuel/app/bootstrap.php file: Autoloader::add_namespace('Carbon', APPPATH.'vendor/nesbot/carbon', true);
    Autoloader::add_class('Carbon', APPPATH.'/vendor/nesbot/carbon/Carbon/Carbon.php'); But the class does not appear in the list of defined classes - could someone explain what i'm doing wrong?
  • A PSR-0 compliant composer package always has a namespace with two parts, the vendor and the package name. This doesn't look compliant, it's only a single class, so you should not use the 'true' parameter to indicate a PSR-0 namespace. I use this to load Cabinet:
    Autoloader::add_namespace('Cabinet\\DBAL',APPPATH.'vendor/cabinet/classes/Cabinet/DBAL/', true);
    
    note that the trailing slash is required! There's no need to add the classes, if the namespace is added correctly, adding it will only prevent a file system lookup.
  • Thanks, i've tried adding the following into bootstrap.php, but the class still does not appear in the get_declared_classes list? Autoloader::add_namespace('Carbon', APPPATH.'vendor/nesbot/carbon/Carbon/', false);
  • Once you've mapped the namespace you should be able to load the class. get_declared_classes() lists the classes that are loaded, which doesn't happen until you actually use the class. Fuel doesn't load anything that isn't needed. Hence "autoloader".
  • Ah i see, so how do i actually _use_ the class? Calling Carbon::now() gives me an exception, as does declaring: use Carbon; then trying to use the class. I apologise, my namespace usage knowledge sucks.
  • Ignore that last message, i managed to call the class using the namespace, then the classname, finally! :) thanks for your help. If i want to call it without the namespace, i.e. just Carbon, can i use the add_core_namespace method to bring it into the core namespace, with no negative side effects?

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion