Generating your first composer.json file for your project.
Please download Composer first and install it on your computer. Composer can be downloaded from https://getcomposer.org/download/
Go to your project root folder and create a composer.json file with the following components. The basic components of a composer.json file are as follows. Alternatively, you can use the code below and edit it to suit your project.
name
:- Format:
"vendor/package-name"
. vendor
is typically your username or organization name, andpackage-name
is the project’s name.
- Format:
description
:- A brief description of the project’s purpose.
type
:- Defines the type of the package, which can be
"project"
(for applications) or"library"
(for reusable libraries). Setting it to"project"
is typical when creating standalone applications.
- Defines the type of the package, which can be
require
:- Lists dependencies with version constraints.
- Commonly includes
"php"
to specify compatible PHP versions and other required packages.
autoload
:- Defines the autoloading standard for project classes.
psr-4
autoloading is standard and maps namespaces to directories. In this example,"App\\"
namespace corresponds to thesrc/
directory, making classes insrc/
accessible under theApp
namespace.
scripts
(optional):- Allows custom scripts, like running tests. For example,
"test": "phpunit"
lets you run tests withcomposer test
if you’re using PHPUnit.
- Allows custom scripts, like running tests. For example,
minimum-stability
(optional):- Defines the acceptable stability for packages (
"stable"
,"dev"
,"alpha"
,"beta"
, or"RC"
)."stable"
is usually preferred in production projects to ensure reliability.
- Defines the acceptable stability for packages (
license
:- Specifies the license, such as
"MIT"
,"GPL-3.0"
, etc.
- Specifies the license, such as
{
"name": "vendor/package-name",
"description": "A short description of your project",
"type": "project",
"require": {
"php": ">=7.4"
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"scripts": {
"test": "phpunit"
},
"minimum-stability": "stable",
"license": "MIT"
}
Or Very basic file like,
{
"name": "vendor/package-name",
"require": {
"php": ">=7.4"
}
}