How to generate laravel composer.json from a vendor directory?

27.5k views Asked by At

In my Laravel project the composer.json and composer.lock files were deleted and I don't know how to rebuild the composer.json file from an existing vendor directory .

1

There are 1 answers

1
aimme On BEST ANSWER

Note

downside of using this method is that the main composer.json file would include some of the packages that have been required by existing or sub packages or multiple packages. but don't worry it would be installed only one time. but there might be problem with versions.

Advice

I recommend you use some version control like git to overcome such situations in future.

can't guarantee about the following method i am going to present as its not a usual case. but might work.

create a new composer.json file like this in project root. you could use composer init command if you wish.

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=5.6.4",
        "laravel/framework": "5.3.*"
    },
    "require-dev": {
        "fzaninotto/faker": "~1.4",
        "mockery/mockery": "0.9.*",
        "phpunit/phpunit": "5.6",
        "symfony/css-selector": "3.1.*",
        "symfony/dom-crawler": "3.1.*"
    },
    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "Blog\\": "app/"
        }
    },
    "autoload-dev": {
        "classmap": [
            "tests/TestCase.php"
        ]
    },
    "scripts": {
        "post-root-package-install": [
            "php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "php artisan key:generate"
        ],
        "post-install-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postInstall",
            "php artisan optimize"
        ],
        "post-update-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postUpdate",
            "php artisan optimize"
        ]
    },
    "config": {
        "preferred-install": "dist"
    }
}
  • go to every package folder inside vendor folder and find the package's composer.json file. from that file find the "name" of that package from that composer.json file.
  • add that name of the package to the main composer.json file's "require" part. if you want to add that package to development add it to "require-dev" part.

  • composer install or composer update

  • composer dump-autoload