php - Composer, PSR-4 Autoloading Itteration -
php - Composer, PSR-4 Autoloading Itteration -
i running slim , twig, folder construction follow
/application/modules
in composer.json have
"autoload": { "psr-4": { "application\\": "application/modules/", "core\\": "application/", "middleware\\": "application/slim/middleware/" } }
my issue is in application\modules\
have directory each module. question is, possible create composer iterate through sub directories when using psr-4 autoload?
i see issue psr-4 declaration.
you shouldn't place "core" classes within folder has subfolders other namespaces.
psr-4 autoloading in composer works this: if qualified class name of class load matches prefix declared in of psr-4 definitions, prefix mentioned in prefix removed class name, , remaining class name mapped filename , beingness searched.
if have classes named application\
in folder application/modules
, , have classes named core\
in folder application
, technically composer might find files match class name core\modules\whatever
, although these file contain class application\whatever
instead.
i recommend moving core
classes own folder , point in psr-4 declaration.
the problem original question omit of import information: class , file construction modules?
composer's autoloader happily resolve class starts namespace prefix application
, remove prefix classname, convert remainders path name , search file in application/modules/
. given have module class application\mymodule\foobar
, searched in application/modules/mymodule/foobar.php
. why? because prefix application
removed allow shorter path names in psr-4. (using psr-0 mean have have folder every level of namespace in classname.)
note recommended optimum performance create prefix namespaces long possible, because composer allows have more 1 directory given prefix, has search right file in more 1 directory. searching files time consuming!
php composer-php autoload slim
Comments
Post a Comment