Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add a library to silex

Tags:

php

silex

I know this question has already been asked, but it seems that autoloading process changed a little bit with composer.

I just want to add a class library to my silex project.

So I made this file: vendor\lib\picture.php

<?php
namespace MyNamespace;

class Picture
{
    function testage()
    {
        echo 'hihaaa ça marche'; exit;
    }
}

in vendor/composer/autoload_namespaces.php, I added this line to the big array:

'MyNamespace' => $vendorDir . '/lib/',

And in the main file I added:

use MyNamespace\Picture as Picture;

and called it like that:

$app->register(new Picture());

which gives me this error:

Fatal error: Class 'MyNamespace\Picture' not found...

I just don't know how to add a class that I can use from any controller, easily, without command line (I don't use composer, I downloaded silex preconfigured), any idea?

like image 476
Vilrouge Avatar asked Jul 21 '12 14:07

Vilrouge


2 Answers

If you're using composer you should not change the vendor directory. You should not add files into it, and you should not modify the composer-generated files.

I recommend you put those classes into the src directory. @gunnx shows how you can configure autoloading in composer.json, so that it gets re-generated every time you run composer install.

The file would be in src/MyNamespace/Picture.php. The autoload config in composer.json would be:

{
    "autoload": {
        "psr-0": { "MyNamespace": "src/" }
    }
}

The actual solution is a combination of the two previous answers. But I think it's important to get the details right ;-).

like image 65
igorw Avatar answered Sep 22 '22 19:09

igorw


Your Picture class should be in this file: vendor/lib/MyNamespace/Picture.php. Note the full namespace and the casing.

like image 42
Maerlyn Avatar answered Sep 22 '22 19:09

Maerlyn