Skip to content

Commit 5fef49f

Browse files
committed
docs: add How to Get a Service
1 parent 11e97ad commit 5fef49f

3 files changed

Lines changed: 22 additions & 3 deletions

File tree

user_guide_src/source/concepts/services.rst

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,26 @@ error-resistant.
4848

4949
.. 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.
5050

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+
5164
Convenience Functions
52-
*********************
65+
=====================
5366

5467
Two functions have been provided for getting a service. These functions are always available.
5568

5669
service()
57-
=========
70+
---------
5871

5972
The first is ``service()`` which returns a new instance of the requested service. The only
6073
required parameter is the service name. This is the same as the method name within the Services
@@ -68,7 +81,7 @@ If the creation method requires additional parameters, they can be passed after
6881
.. literalinclude:: services/004.php
6982

7083
single_service()
71-
================
84+
----------------
7285

7386
The second function, ``single_service()`` works just like ``service()`` but returns a new instance of
7487
the class:
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)