diff --git a/system/Common.php b/system/Common.php index 4504501d8d7e..f054815fe1b5 100644 --- a/system/Common.php +++ b/system/Common.php @@ -18,10 +18,12 @@ use CodeIgniter\Database\ConnectionInterface; use CodeIgniter\Debug\Timer; use CodeIgniter\Files\Exceptions\FileNotFoundException; +use CodeIgniter\HTTP\CLIRequest; use CodeIgniter\HTTP\Exceptions\HTTPException; use CodeIgniter\HTTP\IncomingRequest; use CodeIgniter\HTTP\RedirectResponse; use CodeIgniter\HTTP\RequestInterface; +use CodeIgniter\HTTP\Response; use CodeIgniter\HTTP\ResponseInterface; use CodeIgniter\HTTP\URI; use CodeIgniter\Model; @@ -882,6 +884,28 @@ function remove_invisible_characters(string $str, bool $urlEncoded = true): stri } } +if (! function_exists('request')) { + /** + * Returns the shared Request. + * + * @return CLIRequest|IncomingRequest + */ + function request() + { + return Services::request(); + } +} + +if (! function_exists('response')) { + /** + * Returns the shared Response. + */ + function response(): Response + { + return Services::response(); + } +} + if (! function_exists('route_to')) { /** * Given a controller/method string and any params, diff --git a/tests/system/CommonFunctionsTest.php b/tests/system/CommonFunctionsTest.php index faa261190125..53b35962dfd2 100644 --- a/tests/system/CommonFunctionsTest.php +++ b/tests/system/CommonFunctionsTest.php @@ -12,6 +12,7 @@ namespace CodeIgniter; use CodeIgniter\Config\BaseService; +use CodeIgniter\HTTP\CLIRequest; use CodeIgniter\HTTP\IncomingRequest; use CodeIgniter\HTTP\RedirectResponse; use CodeIgniter\HTTP\Response; @@ -137,6 +138,31 @@ public function testRedirectDefault() $this->assertInstanceOf(RedirectResponse::class, redirect()); } + public function testRequestIncomingRequest() + { + Services::createRequest(new App()); + + $request = request(); + + $this->assertInstanceOf(IncomingRequest::class, $request); + } + + public function testRequestCLIRequest() + { + Services::createRequest(new App(), true); + + $request = request(); + + $this->assertInstanceOf(CLIRequest::class, $request); + } + + public function testResponse() + { + $response = response(); + + $this->assertInstanceOf(Response::class, $response); + } + public function testView() { $data = [ diff --git a/user_guide_src/source/changelogs/v4.3.0.rst b/user_guide_src/source/changelogs/v4.3.0.rst index 3d70546a6d8b..e626fccb09c9 100644 --- a/user_guide_src/source/changelogs/v4.3.0.rst +++ b/user_guide_src/source/changelogs/v4.3.0.rst @@ -122,6 +122,7 @@ Helpers and Functions - Now you can autoload helpers by **app/Config/Autoload.php**. - Added new Form helper function :php:func:`validation_errors()`, :php:func:`validation_list_errors()` and :php:func:`validation_show_error()` to display Validation Errors. - You can set the locale to :php:func:`route_to()` if you pass a locale value as the last parameter. +- Added :php:func:`request()` and :php:func:`response()` functions. Others ====== diff --git a/user_guide_src/source/general/common_functions.rst b/user_guide_src/source/general/common_functions.rst index 08653ed1f1b6..444e31511c76 100755 --- a/user_guide_src/source/general/common_functions.rst +++ b/user_guide_src/source/general/common_functions.rst @@ -332,6 +332,24 @@ Miscellaneous Functions .. literalinclude:: common_functions/007.php +.. php:function:: request() + + :returns: The shared Request object. + :rtype: IncomingRequest|CLIRequest + + .. versionadded:: 4.3.0 + + This function is a wrapper for ``Services::request()``. + +.. php:function:: response() + + :returns: The shared Response object. + :rtype: Response + + .. versionadded:: 4.3.0 + + This function is a wrapper for ``Services::response()``. + .. php:function:: route_to($method[, ...$params]) :param string $method: The named route alias, or name of the controller/method to match.