@@ -15,6 +15,11 @@ What are Factories?
1515Like :doc: `./services `, **Factories ** are an extension of autoloading that helps keep your code
1616concise yet optimal, without having to pass around object instances between classes.
1717
18+ Factories are similar to CodeIgniter 3's ``$this->load `` in the following points:
19+
20+ - Load a class
21+ - Share the loaded class instance
22+
1823At its
1924simplest, Factories provide a common way to create a class instance and access it from
2025anywhere. This is a great way to reuse object states and reduce memory load from keeping
@@ -44,8 +49,10 @@ Example
4449Take a look at **Models ** as an example. You can access the Factory specific to Models
4550by using the magic static method of the Factories class, ``Factories::models() ``.
4651
52+ The static method name is called *component *.
53+
4754By default, Factories first searches in the ``App `` namespace for the path corresponding to the magic static method name.
48- ``Factories::models() `` searches the path ** Models/ ** .
55+ ``Factories::models() `` searches the ** app/Models ** directory .
4956
5057In the following code, if you have ``App\Models\UserModel ``, the instance will be returned:
5158
@@ -54,6 +61,7 @@ In the following code, if you have ``App\Models\UserModel``, the instance will b
5461Or you could also request a specific class:
5562
5663.. literalinclude :: factories/002.php
64+ :lines: 2-
5765
5866If you have only ``Blog\Models\UserModel ``, the instance will be returned.
5967But if you have both ``App\Models\UserModel `` and ``Blog\Models\UserModel ``,
@@ -62,6 +70,7 @@ the instance of ``App\Models\UserModel`` will be returned.
6270If you want to get ``Blog\Models\UserModel ``, you need to disable the option ``preferApp ``:
6371
6472.. literalinclude :: factories/010.php
73+ :lines: 2-
6574
6675See :ref: `factories-options ` for the details.
6776
@@ -119,8 +128,8 @@ Key Type Description
119128========== ============== ============================================================ ===================================================
120129component string or null The name of the component (if different than the static ``null `` (defaults to the component name)
121130 method). This can be used to alias one component to another.
122- path string or null The relative path within the namespace/folder to look for ``null `` (defaults to the component name)
123- classes.
131+ path string or null The relative path within the namespace/folder to look for ``null `` (defaults to the component name,
132+ classes. but makes the first character uppercase)
124133instanceOf string or null A required class name to match on the returned instance. ``null `` (no filtering)
125134getShared boolean Whether to return a shared instance of the class or load a ``true ``
126135 fresh one.
@@ -143,6 +152,9 @@ Configurations
143152To set default component options, create a new Config files at **app/Config/Factory.php **
144153that supplies options as an array property that matches the name of the component.
145154
155+ Example: Filters Factories
156+ --------------------------
157+
146158For example, if you want to create **Filters ** by Factories, the component name wll be ``filters ``.
147159And if you want to ensure that each filter is an instance of a class which implements CodeIgniter's ``FilterInterface ``,
148160your **app/Config/Factory.php ** file might look like this:
@@ -155,6 +167,22 @@ and the returned instance will surely be a CodeIgniter's filter.
155167This would prevent conflict of an third-party module which happened to have an
156168unrelated ``Filters `` path in its namespace.
157169
170+ Example: Library Factories
171+ --------------------------
172+
173+ If you want to load your library classes in the **app/Libraries ** directory with
174+ ``Factories::library('SomeLib') ``, the path `Libraries ` is different from the
175+ default path `Library `.
176+
177+ In this case, your **app/Config/Factory.php ** file will look like this:
178+
179+ .. literalinclude :: factories/011.php
180+
181+ Now you can load your libraries with the ``Factories::library() `` method:
182+
183+ .. literalinclude :: factories/012.php
184+ :lines: 2-
185+
158186setOptions Method
159187=================
160188
0 commit comments