Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Using composer packages with FuelPHP
  • I've been using Evert Pot's VObject library with my FuelPHP project. I used composer to install it in my app. In this blog post: http://fuelphp.com/blogs/2013/01/fuelphp-and-composer it says to add the following code to the application bootstrap.php file:

    // load the Composer autoloader
    require APPPATH.'vendor/autoload.php';
    I've verified that the file is generated by composer. It looks like this:

    <?php

    // autoload.php generated by Composer

    require_once __DIR__ . '/composer' . '/autoload_real.php';

    return ComposerAutoloaderInitcbe1e80d94d45153f8eac24740a671f7::getLoader();

    I'm not familiar with the internal workings of composer, but it's not throwing any exceptions. However, when I try to load the package, I get an error that the package couldn't be found.

    I determined a workaround is to create a bootstap.php file in the package directory, and point to an include file in the project that loads all of the files. Is this just a weird package-specific quirk or do I need to do some additional configuration for the composer autoloader to work?
  • You don't need to do that, as of 1.6 FuelPHP already uses Composer, and this is all setup.

    Just add your package to the existing composer.json file that is in your project root, and off you go...
  • I tried that, but there seems to be some sort of configuration issue. Just to make sure that it wasn't something specific with my application, I setup a test environment:

    git clone --recursive git://github.com/fuel/fuel.git -b 1.6/master test
    cd test
    rm CHANGELOG.md CONTRIBUTING.md README.md TESTING.md build.xml
    sudo rm -R .git* docs/
    rm fuel/app/config/config.php
    cp fuel/core/config/config.php fuel/app/config/config.php
    - set timezone and security filters in the config file
    composer update
    cd fuel/app
    vi composer.json
    - insert this text:
    {
        "require" : {
            "sabre/vobject" : "2.0.*"
        }
    }
    composer update
    cd ../..
    vi fuel/app/classes/welcome.php
    - in the action_index() method, add "Package::load('Sabre/Vobject');

    This results in the error message:
    Fuel\Core\PackageNotFoundException [ Error ]: Package 'Sabre/Vobject' could not be found at 'PKGPATH/sabre/vobject/'

    So I opened up fuel/app/config and set:

    'package_paths' => array(
    PKGPATH,
    APPPATH,
    ),

    But that resulted in the error:
    Fuel\Core\PackageNotFoundException [ Error ]: Package 'Sabre/Vobject' could not be found at 'APPPATH/sabre/vobject/'

    So I added the vendor folder:

    'package_paths' => array(
    PKGPATH,
    APPPATH.'vendor'.DS, 
    ),

    This results in the error:
    Fuel\Core\PhpErrorException [ Warning ]: include(/Library/WebServer/Documents/test/fuel/app/vendor/sabre/vobject/bootstrap.php): failed to open stream: No such file or directory

    This is true. There is no bootstrap.php file in that directory. I can create one manually, but I thought this was all supposed to be handled by composer. Am I doing something wrong?
  • Whoops, I almost forgot: the same error appears whether or not this code appears in bootstrap.php:

    require APPPATH.'vendor/autoload.php';


  • Update: if I create an empty bootstrap.php file in the package directory and include the "require APPPATH.'vendor/autoload.php'" code in the main application bootstrap.php file, everything works. Is the bootstrap.php file a requirement of FuelPHP that is not a requirement of composer?
  • HarroHarro
    Accepted Answer
    Ah, you're making wrong assumptions. Composer packages !== FuelPHP Packages.

    A composer package (I prefer the name library) is just a directory structure, containing classes in a folder, and composer provides an autoloader to be able to load those classes.

    This is a generic mechanism, and has nothing to do with FuelPHP specific.

    So if you have a composer library called "Sabre/Vobject", classes probably live in the \
    Sabre\Vobject namespace, and you access them as such.

    If you want to do Fuel specific stuff, you need to create facade's for these classes in the library, which can interface the Fuel environment with the library. For those facade classes you can use a package. Look at Upload, which works this way.
  • Ahh...I see now. The composer autoloader takes care of loading the composer packages or "libraries." I don't have to explicitly call \Package::load() as I would with a FuelPHP package. The composer packages are always available.
  • Correct.

Howdy, Stranger!

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

In this Discussion