Project structure for PHP

后端 未结 5 1158
被撕碎了的回忆
被撕碎了的回忆 2021-01-30 11:39

I am new to PHP and want to know the directory structure for the php projects. I have experience in Java and in java we have src contains java source files, WEB-INF contains lib

5条回答
  •  天命终不由人
    2021-01-30 11:51

    With the invention of Composer, people now have a central place to register their projects for the world to consume, and other people now can look at that code base and see similarities.

    The result is this: https://github.com/php-pds/skeleton

    In short:

    If a package has a root-level directory for ...
                                ... then it MUST be named:
    command-line executables    bin/
    configuration files         config/
    documentation files         docs/
    web server files            public/
    other resource files        resources/
    PHP source code             src/
    test code                   tests/
    

    This standard does not make any further recommendations about which directories have to exist below src or public.

    The task to organize your PHP source files inside any of these directories is still up to you, but there are suggestions outlined in this article that I'd agree on: Either sort by type (controller, entity, service), or sort by feature (user, login, cart, catalog, article, comment) - the latter keeping all the code that belongs to one feature in on directory (or few sub directories), which often seems to be the better file organisation.

    When organizing by type, you'll find yourself jumping between directories quite often, and also you do not get a good overview about what the code is about - you'd always have "Controller", but you rarely have "StampCollection".

提交回复
热议问题