简体   繁体   中英

Symfony2 + Composer (multiple domains)

We are currently migrating our project to symfony2. The website uses multiple domains:

  • domain.com - main website
  • help.domain.com - faq
  • profile.domain.com - for user CP

and so on

To make it work my idea is to create directory 'sites' and for each subdomain create its own directory where it has framework files + vendor directory with symlinks to directories in root vendor/* (every dir except composer and autoload.php file). So the structure looks something like this:

root/
    sites/
        domain.com/
            app/
            src/
            web/
            vendor/
                symlinks for each library to root vendor
            composer.json
            composer.lock
        profile.domain.com/
            app/
            src/
            web/
            vendor/
                symlinks for each library to root vendor
            composer.json
            composer.lock
    vendor/

I needed to do it this way, because autoload has to be abit different for every domain (we store code that is shared between domains in vendor/Company, and code that is unique for each domain is stored in /src folder of domain directory).

The problem is that now i have to do " composer install " at each directory and it creates its own composer.lock and if i later update at any of the directories, the libs will be updated, but composer.lock files will not be synced, so it will just reinstall it each time.

Is there a better way to solve this problem ? (I thought of putting composer only at the root direcotry, but I didn't find a way to dump different autoload.php files for every domain).

You either have one project with a single set of dependencies or two projects with separate deps. Anything in between does not make sense. Seems like you want to go with the first option but have concerns about autoload performance.

The thing is that by default, autoloading will be slow anyways. So even if you had two separate autoload files including only the deps of the specific sub-project, it would probably still be quite slow.

What you really should do is dump a class map, which is basically an autoload file that maps all class names to the files they reside in. This keeps the stat calls down and is thus very efficient. And loading the entire class map in both sub-projects should not give you too much overhead either, especially if you have APC enabled.

You can dump the class map file by using this command:

$ composer dump-autoload --optimize

Now you should get increased autoloading performance.

TLDR: Use a single composer.json with class map autoloading, it is fast.

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