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

Amine picture Amine · Dec 16, 2016 · Viewed 16.1k times · Source

in my laravel project the composer.json and composer.lock files was deleted and i don't know how to rebuild the composer.json file from an existan the vendor directory . please i need some help or at least some guidence that can help me solve this problme

Answer

aimme picture aimme · Dec 16, 2016

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