Creating and Using a Local Composer Package in PHP
This tutorial demonstrates how to set up a local Composer package in PHP by creating project directories, configuring composer.json files for autoload and repositories, requiring the package, and testing it with a sample index.php, illustrated with command snippets and screenshots.
This article provides a step‑by‑step guide to creating a local Composer package for a PHP project, configuring the package and the root project's composer.json, adding the package as a dependency, and verifying the setup with a simple test script.
First, create a working directory for the local package and initialize Composer:
mkdir composerLocal && cd composerLocal composer initNext, create a packages folder and inside it generate the package directory hail812/hello:
mkdir packages && cd packages mkdir -p hail812/hello && cd hail812/hello composer init # press Enter for all promptsEnsure that the name field in the package’s composer.json matches the directory name. Then configure the package’s autoload section and add a repository entry in the root project's composer.json so the local package can be discovered.
The resulting directory structure is shown in the following image:
Example contents of the package’s composer.json (image):
Example contents of the root project’s composer.json (image):
Install the local package as a dependency: composer require hail812/hello:dev-master This command creates a vendor directory in the project, containing the package files.
Finally, create an index.php file to test the autoloaded package (screenshot shown below):
For the full article and additional details, click the “Read Original” link below.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
