Good time of day. Faced such a problem - autoload composera does not work on hosting. Or rather the mapping that I asked in composer.json:

"autoload": { "psr-0": { "Classes": "" } } 

In the root directory there is a classes folder, and there they are further down in folders.

For example, there is code that runs on my local server:

 $catalogClient = Classes\Models\CatalogClient::first('all', array( 'conditions' => array( 'userID = ?', $_SESSION['user_id'] ), 'order' => 'id desc' )); 

On the local server, the composer finds a class by namespace, and on the hosting an error

 Class 'Classes\Models\CatalogClient' not found 

There is an idea that php composer.phar install needs to be done on the hosting in order to generate the correct autoload. But the hoster prohibits the execution of phar archives. I do not think that full paths are generated and stitched in autoloade, as on my local one (like C: ......). Anyway, I'm not sure that this is the problem) And in what - I can not understand) Any ideas? :)

Ps. Tag "Composer", or "Autoload" unfortunately not (PPS. Tried even so

config / environment.php:

 $loader = require_once __DIR__ . "/../vendor/autoload.php"; $loader->add('Classes', __DIR__ . '/../'); 

Well, that is in the loader to say that look for the Classes directory in such a directory .. But something is still wrong :(

  • folder vendor copied to hosting? PS: by the way, yes, the composer tag is quite appropriate! ;) - digi
  • Those classes that are in the vendor folder - they load normally) Well, I say - only the mapping that I told the composer does not work) - papagaj
  • mmm ... if it works on LAN, but not on the server, then I won’t even tell it right away ... what thread is the autoloader used? maybe he caches the path somewhere and this cache was also transferred to the hosting, and not generated there? - digi
  • Maybe it caches the paths, yes, I wrote about this in the question) The problem is that you cannot generate on the hosting from the phar archive) you can, of course, fill in the source code of the composer, create a composer.sh script, and use it)) but it seems to me that for now - this is not the best and surest way) - papagaj
  • I had a problem-solving problem when I ran Symfony2 on IIS, it didn’t work silently at all ... when I looked at the error log, it really just couldn’t download files because for some reason it didn’t have access rights :( I had to set the launch from IIS my name, not from IUSR, try to see the right of access, it may be the case ... - digi


3 answers 3

pick Composer \ Autoload \ ClassLoader, see what paths it is trying to read something there and what you have in these paths ... by the way it seems there is some kind of tweak setting like opendir or open_basedir they like to prevent access to the above mentioned folders, and you vendors are like I’m looking at above ... they can practice this on the shades ... in general, of course, if you took up serious things, then go to VPS;) for the price is almost the same, but it is incommensurately more useful;)

  • The old project decided to refactor) There is still info - now I will write it off - papagaj
  • Move the vendor to a web folder to include $ loader = require_once DIR . "/vendor/autoload.php"; - digi
  • It did not help .. I found a similar problem: stackoverflow.com/questions/15014561/… But I haven’t quite got it yet) - papagaj
  • then try writing with pens for example require '/var/www/xxx/public_html/xxx/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Resources/stubs/SessionHandlerInterface.php' can you load it in principle? - digi
  • one
    then go to findFile () and stumble any print_r () into it :) see why he doesn't want to load;) - digi

Composer generates the following file:

vendor / composer / autoload_namespaces.php:

 <?php // autoload_namespaces.php generated by Composer $vendorDir = dirname(__DIR__); $baseDir = dirname($vendorDir); return array( 'Symfony\\Component\\Yaml\\' => $vendorDir . '/symfony/yaml/', 'Symfony\\Component\\HttpFoundation' => $vendorDir . '/symfony/http-foundation/', 'Symfony\\Component\\DependencyInjection\\' => $vendorDir . '/symfony/dependency-injection/', 'SessionHandlerInterface' => $vendorDir . '/symfony/http-foundation/Symfony/Component/HttpFoundation/Resources/stubs', 'Classes' => $baseDir . '/', ); 

In fact - he does not cache anything :)

Update:

 $loader = require_once __DIR__ . "/../vendor/autoload.php"; var_dump($loader->findFile('Classes\Models\CatalogClient')); exit; 

Locally - finds the file, and returns the path to the file.

On hosting - false

What the hell)


Here is the loader itself, and its paths:

 object(Composer\Autoload\ClassLoader)#1 (4) { ["prefixes":"Composer\Autoload\ClassLoader":private]=> array(5) { ["Symfony\Component\Yaml\"]=> array(1) { [0]=> string(60) "/var/www/xxx/public_html/xxx.ru/vendor/symfony/yaml/" } ["Symfony\Component\HttpFoundation"]=> array(1) { [0]=> string(71) "/var/www/xxx/public_html/xxx/vendor/symfony/http-foundation/" } ["Symfony\Component\DependencyInjection\"]=> array(1) { [0]=> string(76) "/var/www/xxx/public_html/xxx/vendor/symfony/dependency-injection/" } ["SessionHandlerInterface"]=> array(1) { [0]=> string(119) "/var/www/xxx/public_html/xxx/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Resources/stubs" } ["Classes"]=> array(1) { [0]=> string(40) "/var/www/xxx/public_html/xxx/" } } 

Ways he puts the right. But why does not find the file, it is already a mystery)


Oh god :) I completely forgot that in unix-like systems, the classes /! = Classes / directory. Unlike vindovoza. That's it :) Renaming the directory in Classes - I gained success :) Thanks for the responsiveness :)

  • The composer itself will not cache anything ... but the bootloader that is received from the composer can be wrapped into something, for example at github.com/symfony/ClassLoader/blob/master/… although I myself use only ApcClassLoader - digi
  • Sobsna "xxx" means hidden by user :) - papagaj

In composer.json psr-0, do not write in capital, where I read that it is important. And you have everything right.