Search code examples
phpftpcomposer-phpproduction

Composer vendor/ folder FTP upload


I have a local project with loaded with Composer libs. I uploaded this project to my FTP and received errors, connected with not found classes from some libs.

Can I simply copy vendor/ folder to FTP or I missed something?

Error received: Fatal error: Class 'AAA\Core\Tools' not found in /home/aaa/public_html/api.php on line 11

api.php:

<?php
    use AAA\Core\Tools;

    require_once("./vendor/autoload.php");
    require_once("./api/" . Tools::getFieldValue('controller') . ".php");

All works fine on localhost!


Solution

  • Linux has a case sensitive file system. That means that files Alex.txt and alex.txt are the same thing in Windows, but not in Linux. Actually on Linux both can happily live in the same directory:

    $ tree .                                                    
    .
    ├── alex.txt
    └── Alex.txt
    
    0 directories, 2 files
    

    Taking this into consideration, I would try to double check that the paths you are using in your namespace are actually identical to what is found at the file system level. (i.e: AAA directory with only uppercase letters ; Core directory capitalized and Tools.php file capitalized)


    If you want to keep your existing file system layout, you can use PSR-4 to explicitly tell Composer how to map the namespace to the file system:

    1. Change autoload section from your composer.json:

      {
          "autoload": {
              "psr-4": {"AAA\\DB\\": "db/"}
          }
      }
      

      where db/ is the actual path from the file system

    2. Update the autoloader:

      $ composer dump-autoload
      

    This will take care of directory names, but doesn't apply for files. Meaning that any file inside db/ must be named exactly as used in namespace (for a usage as use AAA\DB\DBCore the file must be db/DBCore.php).

    If your file is named dbcore.php and you want to reference it in your namespace as DBCore, you can use classmap feature from Composer:

    "autoload": {
        "classmap": [
            "db/dbcore.php"
        ]
    }
    

    dbcore.php:

    <?php
    
    namespace AAA\DB;
    
    class DBCore
    {
    }
    

    And you can use it as usual:

    <?php
    require_once("./vendor/autoload.php");
    $dbCore = new \AAA\DB\DBCore();