Home » Php » php – where do I put composer.json

php – where do I put composer.json

Posted by: admin July 12, 2020 Leave a comment

Questions:

I have installed composer.

My project dir tree looks something like this

/home/myproject/public_html/myproject.com

I initially installed it in:

/home/myproject/public_html/myproject.com/bin/composer/

But later moved it to:

/home/myproject/usr/local/bin/composer

Questions:

  1. Where to I create composer.json ?
  2. In the official docs they mention that in order to install new packages I need to write a require key in the json format in that file, does this mean that I dont have to upload the package through ftp?
  3. The docs further say that I can simply install dependencies like ths:

    php composer.phar install

I dont understand the workflow of this process (im fairly new).. what exactly do I need to do to get some packages going (like Respect)

How to&Answers:

Composer has 2 basic elements for you to consider:

  1. The composer.php file itself – this can be located anywhere on your system – usually it is convenient to have it in you search path so you can invoke it by name (no path) from the command line.

  2. Composer.json – this file is the configuration for your project. This is usually best located at the top level of your project. Ideally this is a directory outside the scope of your web server – so that it will never be exposed or served.

Symfony2 has some great documentation and examples of composer in use.

Also be aware that some packages you reference via composer will themselves have composer files – to ensure they match your required dependancies – and they may also have their own dependancies that need to be considered.

I would install composer.json in the following

/home/myproject/composer.json

It would be out of scope of the web server and could be used to manage many assets e.g.

  public_html/
  libs/
  config/
  docs/
  vendor/

Answer:

Where to I create composer.json ?

You should create composer.json to your project root like /home/myproject/public_html/myproject.com/composer.json. If all files of your application live inside your myproject.com folder.

In the official docs they mention that in order to install new
packages I need to write a require key in the json format in that
file, does this mean that I dont have to upload the package through
ftp?

Yes as long as you’re not in shared hosting because most of them don’t allow CLI (SSH).

The docs further say that I can simply install dependencies like this

php composer.phar install

Yes you can simple type the above command and composer.json will install the latest version of your package.

Composer.json (Respect Package)

{
    "require": {
        "respect/validation": "dev-master"
    }
}

Now run composer install will install the require package.

For further packages

{
    "require": {
        "respect/validation": "dev-master",
        "doctrine/orm": "2.*"
    }
}

Now run update composer update it will download the doctrine/orm as well.