|
| 1 | +################### |
| 2 | +Context |
| 3 | +################### |
| 4 | + |
| 5 | +.. contents:: |
| 6 | + :local: |
| 7 | + :depth: 2 |
| 8 | + |
| 9 | +*********** |
| 10 | +What is it? |
| 11 | +*********** |
| 12 | + |
| 13 | +The Context class provides a simple, convenient way to store and retrieve user-defined data throughout a single request. It functions as a key-value store that can hold any data you need to access across different parts of your application during the request lifecycle. |
| 14 | + |
| 15 | +The Context class is particularly useful for: |
| 16 | + |
| 17 | +- Storing request-specific metadata (user IDs, request IDs, correlation IDs) |
| 18 | +- Passing data between filters, controllers, and other components |
| 19 | +- Adding contextual information to your logs automatically |
| 20 | +- Storing sensitive data that should not appear in logs |
| 21 | + |
| 22 | +****************** |
| 23 | +Accessing Context |
| 24 | +****************** |
| 25 | + |
| 26 | +You can access the Context service anywhere in your application using the ``service()`` function: |
| 27 | + |
| 28 | +.. literalinclude:: context/001.php |
| 29 | + |
| 30 | +********************* |
| 31 | +Setting Context Data |
| 32 | +********************* |
| 33 | + |
| 34 | +Setting a Single Value |
| 35 | +====================== |
| 36 | + |
| 37 | +You can store a single key-value pair using the ``set()`` method: |
| 38 | + |
| 39 | +.. literalinclude:: context/002.php |
| 40 | + |
| 41 | +Setting Multiple Values |
| 42 | +======================= |
| 43 | + |
| 44 | +You can also set multiple values at once by passing an array: |
| 45 | + |
| 46 | +.. literalinclude:: context/003.php |
| 47 | + |
| 48 | +The ``set()`` method returns the Context instance, allowing you to chain multiple calls: |
| 49 | + |
| 50 | +.. literalinclude:: context/004.php |
| 51 | + |
| 52 | +********************* |
| 53 | +Getting Context Data |
| 54 | +********************* |
| 55 | + |
| 56 | +Retrieving a Single Value |
| 57 | +========================== |
| 58 | + |
| 59 | +Use the ``get()`` method to retrieve a value by its key: |
| 60 | + |
| 61 | +.. literalinclude:: context/005.php |
| 62 | + |
| 63 | +You can provide a default value as the second parameter, which will be returned if the key doesn't exist: |
| 64 | + |
| 65 | +.. literalinclude:: context/006.php |
| 66 | + |
| 67 | +Retrieving All Data |
| 68 | +=================== |
| 69 | + |
| 70 | +To get all stored context data: |
| 71 | + |
| 72 | +.. literalinclude:: context/007.php |
| 73 | + |
| 74 | +Retrieving Specific Keys |
| 75 | +========================= |
| 76 | + |
| 77 | +You can retrieve only specific keys using ``getOnly()``: |
| 78 | + |
| 79 | +.. literalinclude:: context/008.php |
| 80 | + |
| 81 | +If you need all data except specific keys, use ``getExcept()``: |
| 82 | + |
| 83 | +.. literalinclude:: context/009.php |
| 84 | + |
| 85 | +********************** |
| 86 | +Checking for Data |
| 87 | +********************** |
| 88 | + |
| 89 | +You can check if a key exists in the context: |
| 90 | + |
| 91 | +.. literalinclude:: context/010.php |
| 92 | + |
| 93 | +Or check if a key is missing (the opposite of ``has()``): |
| 94 | + |
| 95 | +.. literalinclude:: context/011.php |
| 96 | + |
| 97 | +********************* |
| 98 | +Removing Context Data |
| 99 | +********************* |
| 100 | + |
| 101 | +Removing a Single Value |
| 102 | +======================== |
| 103 | + |
| 104 | +You can remove data from the context using the ``remove()`` method: |
| 105 | + |
| 106 | +.. literalinclude:: context/012.php |
| 107 | + |
| 108 | +Removing Multiple Values |
| 109 | +========================= |
| 110 | + |
| 111 | +To remove multiple keys at once, pass an array: |
| 112 | + |
| 113 | +.. literalinclude:: context/013.php |
| 114 | + |
| 115 | +Clearing All Data |
| 116 | +================= |
| 117 | + |
| 118 | +To remove all context data: |
| 119 | + |
| 120 | +.. literalinclude:: context/014.php |
| 121 | + |
| 122 | +********************* |
| 123 | +Hidden Context Data |
| 124 | +********************* |
| 125 | + |
| 126 | +The Context class provides a separate storage area for sensitive data that should not be included in logs. This is useful for storing API keys, passwords, tokens, or other sensitive information that you need to access during the request but don't want to expose in log files. |
| 127 | + |
| 128 | +Setting Hidden Data |
| 129 | +=================== |
| 130 | + |
| 131 | +Use the ``setHidden()`` method to store sensitive data: |
| 132 | + |
| 133 | +.. literalinclude:: context/015.php |
| 134 | + |
| 135 | +You can also set multiple hidden values at once: |
| 136 | + |
| 137 | +.. literalinclude:: context/016.php |
| 138 | + |
| 139 | +Getting Hidden Data |
| 140 | +=================== |
| 141 | + |
| 142 | +Retrieve hidden data using ``getHidden()``: |
| 143 | + |
| 144 | +.. literalinclude:: context/017.php |
| 145 | + |
| 146 | +The same methods available for regular data also work with hidden data: |
| 147 | + |
| 148 | +.. literalinclude:: context/018.php |
| 149 | + |
| 150 | +Checking Hidden Data |
| 151 | +==================== |
| 152 | + |
| 153 | +Check if a hidden key exists: |
| 154 | + |
| 155 | +.. literalinclude:: context/019.php |
| 156 | + |
| 157 | +Removing Hidden Data |
| 158 | +==================== |
| 159 | + |
| 160 | +Remove hidden data using ``removeHidden()``: |
| 161 | + |
| 162 | +.. literalinclude:: context/020.php |
| 163 | + |
| 164 | +Clearing Hidden Data |
| 165 | +==================== |
| 166 | + |
| 167 | +To clear all hidden data without affecting regular context data: |
| 168 | + |
| 169 | +.. literalinclude:: context/021.php |
| 170 | + |
| 171 | +To clear both regular and hidden data: |
| 172 | + |
| 173 | +.. literalinclude:: context/022.php |
| 174 | + |
| 175 | +.. important:: Regular data and hidden data are stored separately. A key can exist in both regular and hidden storage with different values. Use ``get()`` for regular data and ``getHidden()`` for hidden data. |
| 176 | + |
| 177 | +*********************************** |
| 178 | +Integration with Logging |
| 179 | +*********************************** |
| 180 | + |
| 181 | +The Context class integrates seamlessly with CodeIgniter's logging system. When enabled, context data is automatically appended to log messages, providing additional information for debugging and monitoring. |
| 182 | + |
| 183 | +Enabling Global Context Logging |
| 184 | +================================ |
| 185 | + |
| 186 | +To enable automatic logging of context data, set the ``$logGlobalContext`` property to ``true`` in your **app/Config/Logger.php** file: |
| 187 | + |
| 188 | +.. literalinclude:: context/023.php |
| 189 | + |
| 190 | +When enabled, all context data (excluding hidden data) will be automatically appended to your log messages as JSON: |
| 191 | + |
| 192 | +.. literalinclude:: context/024.php |
| 193 | + |
| 194 | +This would produce a log entry like: |
| 195 | + |
| 196 | +.. code-block:: text |
| 197 | +
|
| 198 | + ERROR - 2026-02-18 --> Payment processing failed {"user_id":123,"transaction_id":"txn_12345"} |
| 199 | +
|
| 200 | +.. note:: Hidden data set with ``setHidden()`` is **never** included in logs, even when ``$logGlobalContext`` is enabled. This ensures sensitive information like API keys or tokens remain secure. |
| 201 | + |
| 202 | +*************** |
| 203 | +Important Notes |
| 204 | +*************** |
| 205 | + |
| 206 | +- Context data persists only for the duration of a single request. It is not shared between requests. |
| 207 | +- The Context service is shared by default, meaning there is one instance per request. |
| 208 | +- Hidden data is never included in logs, regardless of the logging configuration. |
| 209 | +- Regular context data and hidden context data are stored separately and can have overlapping keys. |
| 210 | +- Context is cleared automatically at the end of each request. |
| 211 | +- In testing environments, remember to clear context data between tests using ``clearAll()`` to ensure test isolation. |
| 212 | + |
| 213 | +*************** |
| 214 | +Class Reference |
| 215 | +*************** |
| 216 | + |
| 217 | +.. php:namespace:: CodeIgniter\Context |
| 218 | +
|
| 219 | +.. php:class:: Context |
| 220 | +
|
| 221 | + .. php:method:: set($key[, $value = null]) |
| 222 | +
|
| 223 | + :param array|string $key: The key or an array of key-value pairs |
| 224 | + :param mixed $value: The value to store (ignored if $key is an array) |
| 225 | + :returns: Context instance for method chaining |
| 226 | + :rtype: Context |
| 227 | + |
| 228 | + Sets one or more key-value pairs in the context. |
| 229 | + |
| 230 | + .. php:method:: setHidden($key[, $value = null]) |
| 231 | +
|
| 232 | + :param array|string $key: The key or an array of key-value pairs |
| 233 | + :param mixed $value: The value to store (ignored if $key is an array) |
| 234 | + :returns: Context instance for method chaining |
| 235 | + :rtype: Context |
| 236 | + |
| 237 | + Sets one or more key-value pairs in the hidden context. |
| 238 | + |
| 239 | + .. php:method:: get($key[, $default = null]) |
| 240 | +
|
| 241 | + :param string $key: The key to retrieve |
| 242 | + :param mixed $default: Default value if key doesn't exist |
| 243 | + :returns: The value or default |
| 244 | + :rtype: mixed |
| 245 | + |
| 246 | + Gets a value from the context. |
| 247 | + |
| 248 | + .. php:method:: getHidden($key[, $default = null]) |
| 249 | +
|
| 250 | + :param string $key: The key to retrieve |
| 251 | + :param mixed $default: Default value if key doesn't exist |
| 252 | + :returns: The value or default |
| 253 | + :rtype: mixed |
| 254 | + |
| 255 | + Gets a value from the hidden context. |
| 256 | + |
| 257 | + .. php:method:: getOnly($keys) |
| 258 | +
|
| 259 | + :param array|string $keys: Key or array of keys to retrieve |
| 260 | + :returns: Array of key-value pairs |
| 261 | + :rtype: array |
| 262 | + |
| 263 | + Gets only the specified keys from the context. |
| 264 | + |
| 265 | + .. php:method:: getOnlyHidden($keys) |
| 266 | +
|
| 267 | + :param array|string $keys: Key or array of keys to retrieve |
| 268 | + :returns: Array of key-value pairs |
| 269 | + :rtype: array |
| 270 | + |
| 271 | + Gets only the specified keys from the hidden context. |
| 272 | + |
| 273 | + .. php:method:: getExcept($keys) |
| 274 | +
|
| 275 | + :param array|string $keys: Key or array of keys to exclude |
| 276 | + :returns: Array of key-value pairs |
| 277 | + :rtype: array |
| 278 | + |
| 279 | + Gets all context data except the specified keys. |
| 280 | + |
| 281 | + .. php:method:: getExceptHidden($keys) |
| 282 | +
|
| 283 | + :param array|string $keys: Key or array of keys to exclude |
| 284 | + :returns: Array of key-value pairs |
| 285 | + :rtype: array |
| 286 | + |
| 287 | + Gets all hidden context data except the specified keys. |
| 288 | + |
| 289 | + .. php:method:: getAll() |
| 290 | +
|
| 291 | + :returns: All context data |
| 292 | + :rtype: array |
| 293 | + |
| 294 | + Gets all data from the context. |
| 295 | + |
| 296 | + .. php:method:: getAllHidden() |
| 297 | +
|
| 298 | + :returns: All hidden context data |
| 299 | + :rtype: array |
| 300 | + |
| 301 | + Gets all data from the hidden context. |
| 302 | + |
| 303 | + .. php:method:: has($key) |
| 304 | +
|
| 305 | + :param string $key: The key to check |
| 306 | + :returns: True if key exists, false otherwise |
| 307 | + :rtype: bool |
| 308 | + |
| 309 | + Checks if a key exists in the context. |
| 310 | + |
| 311 | + .. php:method:: hasHidden($key) |
| 312 | +
|
| 313 | + :param string $key: The key to check |
| 314 | + :returns: True if key exists, false otherwise |
| 315 | + :rtype: bool |
| 316 | + |
| 317 | + Checks if a key exists in the hidden context. |
| 318 | + |
| 319 | + .. php:method:: missing($key) |
| 320 | +
|
| 321 | + :param string $key: The key to check |
| 322 | + :returns: True if key doesn't exist, false otherwise |
| 323 | + :rtype: bool |
| 324 | + |
| 325 | + Checks if a key doesn't exist in the context. Opposite of ``has()``. |
| 326 | + |
| 327 | + .. php:method:: missingHidden($key) |
| 328 | +
|
| 329 | + :param string $key: The key to check |
| 330 | + :returns: True if key doesn't exist, false otherwise |
| 331 | + :rtype: bool |
| 332 | + |
| 333 | + Checks if a key doesn't exist in the hidden context. Opposite of ``hasHidden()``. |
| 334 | + |
| 335 | + .. php:method:: remove($key) |
| 336 | +
|
| 337 | + :param array|string $key: The key or array of keys to remove |
| 338 | + :returns: Context instance for method chaining |
| 339 | + :rtype: Context |
| 340 | + |
| 341 | + Removes one or more keys from the context. |
| 342 | + |
| 343 | + .. php:method:: removeHidden($key) |
| 344 | +
|
| 345 | + :param array|string $key: The key or array of keys to remove |
| 346 | + :returns: Context instance for method chaining |
| 347 | + :rtype: Context |
| 348 | + |
| 349 | + Removes one or more keys from the hidden context. |
| 350 | + |
| 351 | + .. php:method:: clear() |
| 352 | +
|
| 353 | + :returns: Context instance for method chaining |
| 354 | + :rtype: Context |
| 355 | + |
| 356 | + Clears all data from the context (does not affect hidden data). |
| 357 | + |
| 358 | + .. php:method:: clearHidden() |
| 359 | +
|
| 360 | + :returns: Context instance for method chaining |
| 361 | + :rtype: Context |
| 362 | + |
| 363 | + Clears all data from the hidden context (does not affect regular data). |
| 364 | + |
| 365 | + .. php:method:: clearAll() |
| 366 | +
|
| 367 | + :returns: Context instance for method chaining |
| 368 | + :rtype: Context |
| 369 | + |
| 370 | + Clears all data from both the context and hidden context. |
0 commit comments