Where should the composer.json and composer.lock files should be? - php

I've never used Composer before, but installed it so I could set twig up the recommended way.
I now have composer.json and composer.lock files in two separate directories. How do I determine where they SHOULD be? I assume they certainly shouldn't be in my public html root, as one of them has managed to be?

The composer.json file should be created at the topmost directory of your project, i.e. the top directory that is in version control. Subdirectories of this directory likely are for example the "public" or "htdocs" or "www" directory which is the document root for the web server.
By using Composer, this directory will also get a "vendor" directory where the packages are being placed, as well as the composer.lock file which records the exact versions being installed.
Do commit both composer.json and composer.lock. Don't commit the vendor folder, put it onto the ignore list.

Related

Is it possible to validate vendor folder integrity in composer?

I just inherited a composer project in a very bad shape. They sent me a zip file with the vendor directory in it and I suspect that the previous developer has edited files directly inside vendor.
Is there a way to "validate" the vendor folder to ensure that the files inside are unmodified?
Change the name of the old vendor to something else.
Execute composer install again.
Run diff to compare both directories.
E.g. for a sample project where I intentionally modified a single file inside vendor.
$ mv vendor vendor_old
$ composer install
### install output...
$ diff -rq vendor vendor_old
Files vendor/autoload.php and vendor_old/autoload.php differ
Files vendor/composer/autoload_files.php and vendor_old/composer/autoload_files.php differ
Files vendor/composer/autoload_real.php and vendor_old/composer/autoload_real.php differ
Files vendor/composer/autoload_static.php and vendor_old/composer/autoload_static.php differ
Files vendor/symfony/console/Terminal.php and vendor_old/symfony/console/Terminal.php differ
You can mostly ignore the changes to the autoload* files, but with this listing you can concentrate in those other files that report differences (and run a more exhaustive diff from them).
In the example, only vendor/symfony/console/Terminal.php was actually modified.
Copy the project into some other folder, and delete the vendor directory. Run composer install and compare two vendor files.
The easiest way to do this is by using composer status command.
The prerequisite is that package is installed from source (as described on the official Composer site):
If you often need to modify the code of your dependencies and they are installed from source, the status command allows you to check if you have local changes in any of them.

Symfony .gitignore ignores too much

I have a Symfony 3 project.
In the root dir is the usual vendor dir that composer uses for installation.
In the web dir I also have a vendor dir for bootstrap, fontawesome etc.
In .gitignore I have an entry vendor.
Unfortunately both vendor dirs are ignored. But I want to commit the web/vendor dir.
How is that possible?
May be I have found a solution. I added !web/vendor to the .gitignore.
Is this the right way?
gitignore will recursively ignore everything that matches the "vendor" pattern if that is all you have in your gitnore.
There are some basic rules about gitignore you should familiarize yourself with.
The one here is that gitignore will run relative to the directory it's in.
So, if you add a /vendor entry to the .gitignore in your root dir, then it will only ignore the vendor directory in your root dir. Leave out the slash, and it will drill down as far as it can go and ignore everything that has a vendor in the name, whether it be a file, directory, whatever.
A tip: try looking up sample .gitignore files on github for a specific project type you're using. These are usually good enough to get going.
For symfony, a good one is here: https://github.com/github/gitignore/blob/master/Symfony.gitignore
Yes, that was the right way. Just add !web/vendor.

Composer is downloading all files in the php folder rather than in my project root folder?

I'm new to PHP and I was learning my first video of how to check out and make a transaction.
I have installed composer in the xampp/php/ directory. Now when I ran this query
'composer require stripe/stripe-php'
it created all the files like vendor,composer.json and composer.lock in the php folder. How to make it download them all in the root folder of my project?
sorry for my bad English but I hope you get my point.

Composer.json vendor folder on Heroku

I am migrating from shared web hosting to Heroku and can't figure out how to user Composer properly.
I have my app pointing to public_html folder, with the composer.json file in the root folder above this. This would generate a vendor folder in the root folder, which (for reasons I don't understand) I was unable to access when I tried including the autoload.php file in my code (e.g. require_once('../vendor/autoload.php')).
My solution to this was to move composer.json into public_html, but that caused no end of problems with Heroku not finding it, so I moved it back. I then changed the vendor directory in composer.json to point to public_html/vendor.
This had the desired effect, except now my app won't run because it can't find PHP, presumably something to do with the new location of vendor.
Is there a way to either:
Configure composer.json to have Heroku put everything except PHP in public_html/vendor, and put PHP into a root vendor directory, or
Access files above public_html from within my code to allow me to include autoload.php?
OK, it turns out it was a relatively simple fix that I just figured out myself. I'll post it here in case anybody else happens to make the same mistake.
If you change vendor-dir in composer.json, you also need to change the Procfile from
web: vendor/bin/heroku-php-apache2 public_html/
to
web: public_html/vendor/bin/heroku-php-apache2 public_html/

Gitignore error_log.txt

What rules should i add to .gitignore to ignore all error_log.txt files generated in php?
I have tried adding simply error_log to it, but it doesn't seem to work.
Im working on a Win7 x64 and i am new to Git.
To ignore a file (like error_log.txt) in all directories of your repository, you should add the following rule to the .gitignore in the root folder of your repo:
error_log.txt
As said, that will ignore all files called error_log.txt in all directories and subdirectories of the repo.
Be aware, that .gitignore does only ignore files that are not tracked yet. If you have added and commited a error_log.txt already, that file will stay tracked. See Ignore files that have already been committed to a Git repository for futher detail on that.

Categories