You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: user_guide_src/source/concepts/services.rst
+41-13Lines changed: 41 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,12 +7,21 @@ Services
7
7
:depth: 2
8
8
9
9
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.
11
17
12
18
All of the core classes within CodeIgniter are provided as "services". This simply means that, instead
13
19
of hard-coding a class name to load, the classes to call are defined within a very simple
14
20
configuration file. This file acts as a type of factory to create new instances of the required class.
15
21
22
+
Why use Services?
23
+
=================
24
+
16
25
A quick example will probably make things clearer, so imagine that you need to pull in an instance
17
26
of the Timer class. The simplest method would simply be to create a new instance of that class:
18
27
@@ -39,11 +48,27 @@ error-resistant.
39
48
40
49
.. 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.
41
50
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
+
42
64
Convenience Functions
43
-
---------------------
65
+
=====================
44
66
45
67
Two functions have been provided for getting a service. These functions are always available.
46
68
69
+
service()
70
+
---------
71
+
47
72
The first is ``service()`` which returns a new instance of the requested service. The only
48
73
required parameter is the service name. This is the same as the method name within the Services
49
74
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
55
80
56
81
.. literalinclude:: services/004.php
57
82
83
+
single_service()
84
+
----------------
85
+
58
86
The second function, ``single_service()`` works just like ``service()`` but returns a new instance of
59
87
the class:
60
88
61
89
.. literalinclude:: services/005.php
62
90
63
91
Defining Services
64
-
=================
92
+
*****************
65
93
66
94
To make services work well, you have to be able to rely on each class having a constant API, or
67
95
`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``:
75
103
76
104
.. literalinclude:: services/006.php
77
105
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``
79
107
instead of ``CodeIgniter\Router\RouterCollection``:
80
108
81
109
.. literalinclude:: services/007.php
82
110
83
111
Allowing Parameters
84
-
-------------------
112
+
===================
85
113
86
114
In some instances, you will want the option to pass a setting to the class during instantiation.
87
115
Since the services file is a very simple class, it is easy to make this work.
88
116
89
117
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
91
119
changing that path, though, if their needs require it. So the class accepts the ``$viewPath``
92
120
as a constructor parameter. The service method looks like this:
93
121
@@ -99,7 +127,7 @@ the path it uses:
99
127
.. literalinclude:: services/009.php
100
128
101
129
Shared Classes
102
-
-----------------
130
+
==============
103
131
104
132
There are occasions where you need to require that only a single instance of a service
105
133
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
110
138
.. literalinclude:: services/010.php
111
139
112
140
Service Discovery
113
-
-----------------
141
+
*****************
114
142
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.
116
144
This allows simple use of any module Services files. In order for custom Services files to be discovered, they must
117
145
meet these requirements:
118
146
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**
121
149
- It must extend ``CodeIgniter\Config\BaseService``
122
150
123
151
A small example should clarify this.
124
152
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,
126
154
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:
0 commit comments