Add library to Silex

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

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

So, I made this file: provider \ 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 a large array:

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

And in the main file, I added:

 use MyNamespace\Picture as Picture; 

and is called like this:

 $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 the command line (I do not use composer, I loaded silex preconfigured), any idea?

+6
source share
3 answers

If you use a composer, you should not change the supplier directory. You should not add files to it, and you should not modify files created using the composer.

I recommend that you put these classes in the src directory. @gunnx shows how you can configure autoload in composer.json so that it will be generated every time composer install run.

The file will be located in src/MyNamespace/Picture.php . The startup configuration in composer.json will be:

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

The actual solution is a combination of the two previous answers. But I think it is important for the details to be set correctly; -).

+12
source

Your image class must be in this file: vendor/lib/MyNamespace/Picture.php . Note the full namespace and case.

+2
source

You can add your own code to the autoloader by adding the following to your composer.json for example.

 { "autoload": { "psr-0": {"Acme": "src/"} } 
+2
source

Source: https://habr.com/ru/post/921029/


All Articles