Skip to content

Commit 5850f1e

Browse files
committed
docs: add Creating Composer Packages
1 parent 621edbe commit 5850f1e

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-1
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
##########################
2+
Creating Composer Packages
3+
##########################
4+
5+
You can make the :doc:`../general/modules` you create into Composer packages,
6+
or can create a Composer package for CodeIgniter 4.
7+
8+
.. contents::
9+
:local:
10+
:depth: 2
11+
12+
****************
13+
Folder Structure
14+
****************
15+
16+
Here's a typical directory structure for a Composer package::
17+
18+
your-package-name/
19+
├── README.md
20+
├── composer.json
21+
├── src/
22+
│   └── YourClass.php
23+
└── tests/
24+
└── YourClassTest.php
25+
26+
**********************
27+
Creating composer.json
28+
**********************
29+
30+
In the root of your package directory, create a **composer.json** file. This file
31+
defines metadata about your package and its dependencies.
32+
33+
The ``composer init`` command helps you create it.
34+
35+
For example, **composer.json** might look like this::
36+
37+
{
38+
"name": "your-vendor/your-package",
39+
"description": "Your package description",
40+
"type": "library",
41+
"license": "MIT",
42+
"autoload": {
43+
"psr-4": {
44+
"YourVendor\\YourPackage\\": "src/"
45+
}
46+
},
47+
"authors": [
48+
{
49+
"name": "Your Name",
50+
"email": "[email protected]"
51+
}
52+
],
53+
"require": {
54+
// Any dependencies required by your package go here
55+
},
56+
"require-dev": {
57+
// Any development dependencies (e.g., PHPUnit) go here
58+
}
59+
}
60+
61+
Package Name
62+
============
63+
64+
The ``name`` field is important here. Package names are generally written in the
65+
format "vendor-name/package-name" with all lowercase. Here is a common example:
66+
67+
- ``your-vendor-name``: The name that identifies the vendor (creator of the package),
68+
such as the name of you or your organization.
69+
- ``your-package-name``: The name of the package you are creating.
70+
71+
Thus, it is important to make the name unique and distinguish it from other packages.
72+
Uniqueness is especially important when publishing.
73+
74+
Namespace
75+
=========
76+
77+
The package name then determines the vendor namespace in ``autoload.psr4``.
78+
79+
If your package name is ``your-vendor/your-package``, the vendor namespace must
80+
be ``YourVendor``. So you would write like the following::
81+
82+
"autoload": {
83+
"psr-4": {
84+
"YourVendor\\YourPackage\\": "src/"
85+
}
86+
},
87+
88+
This setting instructs Composer to autoload the source code for your package.
89+
90+
************
91+
Config Files
92+
************
93+
94+
Allowing Users to Override Settings
95+
===================================
96+
97+
If your package has a configuration file and you want users to be able to override
98+
the settings, use :php:func:`config()` with the short classname like ``config('YourConfig')``
99+
to call the configuration file.
100+
101+
Users can then override the package configuration by placing a configuration class
102+
with the same short classname in **app/Config** that extends the package Config
103+
class like ``YourVendor\YourPackage\Config\YourConfig``.
104+
105+
Overriding Settings in app/Config
106+
=================================
107+
108+
If you need to override or add to known configurations in the **app/Config** folder,
109+
you can use :ref:`Implicit Registrars <registrars>`.

user_guide_src/source/extending/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ CodeIgniter 4 has been designed to be easy to extend or build upon.
1212
events
1313
basecontroller
1414
authentication
15+
composer_packages
1516
contributing

user_guide_src/source/general/modules.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ Code Modules
33
############
44

55
CodeIgniter supports a form of code modularization to help you create reusable code. Modules are typically
6-
centered around a specific subject, and can be thought of as mini-applications within your larger application. Any
6+
centered around a specific subject, and can be thought of as mini-applications within your larger application.
7+
8+
Any
79
of the standard file types within the framework are supported, like controllers, models, views, config files, helpers,
810
language files, etc. Modules may contain as few, or as many, of these as you like.
911

12+
If you want to create a module as a Composer package, see also :doc:`../extending/composer_packages`.
13+
1014
.. contents::
1115
:local:
1216
:depth: 2

0 commit comments

Comments
 (0)