Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Mayur Pande
PLUS
Mayur Pande
Courses Plus Student 11,711 Points

Where to put composer.json file?

So I have installed composer on my linux machine fine and I can use it globally with XAMPP. My question is each time I create a project should I just create a new composer.json file within the root of that project? I found this explanation that says that, however I am still a little confused whether or not to create a new file for each project that I do.

For example at the moment within my htdocs folder I have two projects and I have uploaded two composer.json files.

Here is an example of what the path directories look like;

htdocs/my_project_1/

running the command ls I get a list of the files and folders;

css (folder)
composer.json
composer.lock
index.php
templates (folder)
img (folder)
vendor(folder)

In my second project;

htdocs/my_project_2/

when running the command ls it lists;

composer.json
composer.lock
index.php
products.php
contact.php
vendor (folder)
img(folder)
css (folder)

Is this the correct way of doing it? Also when uploading to the actual web. Do I just upload the composer.json and composer.lock files instead of the vendor folder?

Mayur Pande
Mayur Pande
Courses Plus Student 11,711 Points

Also forgot to ask about git. If I am also using git, would I simply just commit the composer.json and composer.lock files?

1 Answer

Kevin Korte
Kevin Korte
28,149 Points

Correct, that is how you would do it. Composer.json would be at the root of each project folder, as each project will have it's own set of dependencies. I would not upload the composer.lock file, and I would not commit the composer.lock file. Composer.lock is an auto-generated file that composer will make when it looks at composer.json.

Composer.json tells composer what to download, composer.lock tells composer which version it currently has. Its a way to keep track of what it has currently installed. So each computer that runs composer would generate it's own composer.lock file, so there is no benefit of committing or uploaded any auto-generated files.

Hope thats clear