Skip to content

Commit 53a2a2f

Browse files
authored
Merge pull request #5856 from kenjis/fix-docs-services
docs: improve Services
2 parents 3efc6cd + 1b1d03c commit 53a2a2f

8 files changed

Lines changed: 58 additions & 15 deletions

File tree

user_guide_src/source/concepts/services.rst

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,21 @@ Services
77
:depth: 2
88

99
Introduction
10-
============
10+
************
11+
12+
What is Services?
13+
=================
14+
15+
The **Services** in CodeIgniter 4 provide the functionality to create and share new class instances.
16+
It is implemented as the ``Config\Services`` class.
1117

1218
All of the core classes within CodeIgniter are provided as "services". This simply means that, instead
1319
of hard-coding a class name to load, the classes to call are defined within a very simple
1420
configuration file. This file acts as a type of factory to create new instances of the required class.
1521

22+
Why use Services?
23+
=================
24+
1625
A quick example will probably make things clearer, so imagine that you need to pull in an instance
1726
of the Timer class. The simplest method would simply be to create a new instance of that class:
1827

@@ -39,11 +48,27 @@ error-resistant.
3948

4049
.. note:: It is recommended to only create services within controllers. Other files, like models and libraries should have the dependencies either passed into the constructor or through a setter method.
4150

51+
How to Get a Service
52+
********************
53+
54+
As many CodeIgniter classes are provided as services, you can get them like the following:
55+
56+
.. literalinclude:: services/013.php
57+
58+
The ``$typography`` is an instance of the Typography class, and if you call ``\Config\Services::typography()`` again, you will get the exactly same instance.
59+
60+
If you want to get a new instance of the Typography class, you need to pass ``false`` to the argument ``$getShared``:
61+
62+
.. literalinclude:: services/014.php
63+
4264
Convenience Functions
43-
---------------------
65+
=====================
4466

4567
Two functions have been provided for getting a service. These functions are always available.
4668

69+
service()
70+
---------
71+
4772
The first is ``service()`` which returns a new instance of the requested service. The only
4873
required parameter is the service name. This is the same as the method name within the Services
4974
file always returns a SHARED instance of the class, so calling the function multiple times should
@@ -55,13 +80,16 @@ If the creation method requires additional parameters, they can be passed after
5580

5681
.. literalinclude:: services/004.php
5782

83+
single_service()
84+
----------------
85+
5886
The second function, ``single_service()`` works just like ``service()`` but returns a new instance of
5987
the class:
6088

6189
.. literalinclude:: services/005.php
6290

6391
Defining Services
64-
=================
92+
*****************
6593

6694
To make services work well, you have to be able to rely on each class having a constant API, or
6795
`interface <https://www.php.net/manual/en/language.oop5.interfaces.php>`_, to use. Almost all of
@@ -75,19 +103,19 @@ create a new class that implements the ``RouterCollectionInterface``:
75103

76104
.. literalinclude:: services/006.php
77105

78-
Finally, modify **/app/Config/Services.php** to create a new instance of ``MyRouter``
106+
Finally, modify **app/Config/Services.php** to create a new instance of ``MyRouter``
79107
instead of ``CodeIgniter\Router\RouterCollection``:
80108

81109
.. literalinclude:: services/007.php
82110

83111
Allowing Parameters
84-
-------------------
112+
===================
85113

86114
In some instances, you will want the option to pass a setting to the class during instantiation.
87115
Since the services file is a very simple class, it is easy to make this work.
88116

89117
A good example is the ``renderer`` service. By default, we want this class to be able
90-
to find the views at ``APPPATH.views/``. We want the developer to have the option of
118+
to find the views at ``APPPATH . 'views/'``. We want the developer to have the option of
91119
changing that path, though, if their needs require it. So the class accepts the ``$viewPath``
92120
as a constructor parameter. The service method looks like this:
93121

@@ -99,7 +127,7 @@ the path it uses:
99127
.. literalinclude:: services/009.php
100128

101129
Shared Classes
102-
-----------------
130+
==============
103131

104132
There are occasions where you need to require that only a single instance of a service
105133
is created. This is easily handled with the ``getSharedInstance()`` method that is called from within the
@@ -110,21 +138,21 @@ within the class, and, if not, creates a new one. All of the factory methods pro
110138
.. literalinclude:: services/010.php
111139

112140
Service Discovery
113-
-----------------
141+
*****************
114142

115-
CodeIgniter can automatically discover any Config\\Services.php files you may have created within any defined namespaces.
143+
CodeIgniter can automatically discover any **Config/Services.php** files you may have created within any defined namespaces.
116144
This allows simple use of any module Services files. In order for custom Services files to be discovered, they must
117145
meet these requirements:
118146

119-
- Its namespace must be defined in ``Config\Autoload.php``
120-
- Inside the namespace, the file must be found at ``Config\Services.php``
147+
- Its namespace must be defined in **app/Config/Autoload.php**
148+
- Inside the namespace, the file must be found at **Config/Services.php**
121149
- It must extend ``CodeIgniter\Config\BaseService``
122150

123151
A small example should clarify this.
124152

125-
Imagine that you've created a new directory, ``Blog`` in your root directory. This will hold a **blog module** with controllers,
153+
Imagine that you've created a new directory, **Blog** in your project root directory. This will hold a **Blog module** with controllers,
126154
models, etc, and you'd like to make some of the classes available as a service. The first step is to create a new file:
127-
``Blog\Config\Services.php``. The skeleton of the file should be:
155+
**Blog/Config/Services.php**. The skeleton of the file should be:
128156

129157
.. literalinclude:: services/011.php
130158

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
<?php
22

33
$logger = service('logger');
4+
5+
// The code above is the same as the code below.
6+
$logger = \Config\Services::logger();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
<?php
22

33
$renderer = service('renderer', APPPATH . 'views/');
4+
5+
// The code above is the same as the code below.
6+
$renderer = \Config\Services::renderer(APPPATH . 'views/');
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
<?php
22

33
$logger = single_service('logger');
4+
5+
// The code above is the same as the code below.
6+
$logger = \Config\Services::logger(false);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?php
22

3-
$renderer = \Config\Services::renderer('/shared/views');
3+
$renderer = \Config\Services::renderer('/shared/views/');
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?php
22

3-
$postManager = Config\Services::postManager();
3+
$postManager = \Config\Services::postManager();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
$typography = \Config\Services::typography();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
$typography = \Config\Services::typography(false);

0 commit comments

Comments
 (0)