简体   繁体   中英

Composer dump-autoload, issue

While Working on a project using Laravel 4 to be precise, I decided that i wanted to make my own helper file to house my custom functions.. one of which is this below...

function pr($ar=array(), $bool=false){

   echo '<pre>';
   print_r($ar);
   echo '</pre>';

   if($bool){
      exit;
   }

}

in my composer.json file, just after the autoload: classmap , i added myne, autoload:files -arrar and included my custom file, app/helpers as illustrated below..

            "app/database/migrations",
            "app/database/seeds",
            "app/tests/TestCase.php"
        ],

        "others":[
            "app/helpers.php"
        ]

and i switched to my terminal window and ran the following commands

composer dump-autoload -o  

but i still got errors that my pr() function was undefined... then i tried the artisan alternative... [-o ] to optimize the files

php artisan dump-autoload

but still it refused to work... and then i changed the array name from

"others":[
            "app/helpers.php"
        ]

to

"files":[
            "app/helpers.php"
        ]

then i got the desired response, my code could now see the custom function i wrote, please i'd like to know if there is a pattern i was supposed to follow or otherwise, in my case, i mistook " files ", for " others " and i got errors, but incase, what did i miss here, all i see is just a name-string value for the array representation....

This is how composer works. In autoload section you need to use files when you want to load some files. In my Laravel 5 project I have for example:

"autoload": {
    "classmap": [
        "database",
        "tests/TestCase.php"
    ],
    "psr-4": {
        "App\\": "app/",
        "verify\\": "verify/"
    },
    "files": [
        "app/Helpers/functions.php"
    ]
},

If you look at documentation you will see that you need to use files to load any extra files by autoloader.

According to the official documentation

Currently PSR-0 autoloading, PSR-4 autoloading, classmap generation and files includes are supported. PSR-4 is the recommended way though since it offers greater ease of use (no need to regenerate the autoloader when you add classes).

So the reason that "others" did not work was because it is not supported by composer. "others" is simply meaningless, while "files" actually have a specific autoloading mechanism .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM