Making changes in my vendor folder, changes get deleted when I do composer update

So here’s a problem I’m facing. I make changes to some of my extensions in my vendor folder, however, composer update deletes all my changes and brings in the default extension files, overwriting what I already did.

Am I doing something wrong by editing my files in the vendor folder?

It is the way Composer is expected to do.

Did you create your own repositories of those extensions by forking them? If not, they are not yet yours. Any changes on them should be considered "unofficial" and will be lost on update.

When you want to use some 3rd party extension and customize it, you can do the following:

  1. Fork the extension project into your github repository.

For example, if you want to use "someone/yii2-something", fork it into your github repository as "kevinkt/yii2-something".

  1. Add "repositories" entry to you composer.json at the root level.



  ...

  "repositories": [

    {

      "type": "vcs",

      "url": "https://github.com/kevinkt/yii2-something"

    }

  ],

  "require": [

    ...

    "someone/yii2-something": "*",

    ...

  ],

  ...



  1. Do "composer update", and composer will fetch your extension from your github repository.

  2. Edit your extension in the "vendor" directory.

  3. Commit the changes and push them to your github repository.

What’s the best way to do this if I’ve already made code changes in my vendor folder?

Copy the already changed parts of the vendor directory to somewhere else, and copy them back when you edit your extensions.

Sorry, so just to be clear. I’m basically copying out my vendor folder before composer update, then moving the files back afterwards?

Well, now I’m feeling a bit uneasy.

Have you ever worked with git and github? Are you using some version control system for your own project? If not, you should not try to do what I have suggested. Instead, you should start to learn how to use VCS (e.g. git or mercurial) at once, before carelessly trying to modify the 3rd party code.

OK, I’ll try to explain once more.

  1. Copy the source files in the vendor directory to somewhere outside of your project directory. You don’t need to copy all the files, but only those files that you have modified.

  2. Fork the extension project(s) that you have modified.

For example, if you have modified "someone/yii2-something", fork it into your github repository as "kevinkt/yii2-something". This process should be done in github, using your github account.

And you must repeat the same process for each extension that you have modified.

  1. Add “repositories” entry to your project’s composer.json at the root level.



  ...

  "repositories": [

    {

      "type": "vcs",

      "url": "https://github.com/kevinkt/yii2-something"

    },

    {

      "type": "vcs",

      "url": "https://github.com/kevinkt/yii2-another"

    }

  ],

  "require": [

    ...

    "someone/yii2-something": "*",

    "other-person/yii2-another": "*",

    ...

  ],

  ...



  1. Do "composer update", and composer will fetch your extension(s) from your github repositori(es).

This will overwrite all the existing code of the extensions, as was the case with you.

But there’s a big difference. “someone/yii2-something” now doesn’t point to someone’s Packagist package, but to your own github repository. And the directory itself is a local git repository of yours.

  1. Edit your extension in the sub-directory of the vendor directory as you may like.

Here, you can restore the saved files.

  1. Commit the changes and push them to your github repository.

You can commit the changes to the local repository, and push it to your github repository. By doing so, you can maintain your extension’s source code.

My suggestion is just override required class - make changes and use overrided class in place when u need it.

Never edit vendor folder

Yes, you are right. I usually don’t want to nor need to touch the code inside vendor directory.

But in some rare cases I had to do it as I described above.

Okay this is exactly what I needed. Thank you. This makes total sense.

Yes I use Git. I was just confused by your earlier statement.

  • Kevin

So I tried to do this. However, when I push my changes to Github, the extension’s repository (the one that I just forked) doesn’t get updated with my new files I edited.

I think the problem I’m having is that I’m editing my main project through GitHub, but I don’t have separate local repositories set up for each of my extensions I forked. Is that the issue?

Did you commit your changes?

Sorry, I don’t understand what you say.

If you have followed what I explained, your extensions in ‘vendor’ directory have their own local repositories there.

I committed my changes to my main repository of my app, which contains my vendor folder. Is that how I’m supposed to do it?

“vendor” directory is “ignored” in your main project’s repository. So committing changes to your main repository doesn’t do any changes to your extension’s repository. Do commit in your extension’s repository which is in “vendor” directory.