diff --git a/en/appendices/5-1-upgrade-guide.rst b/en/appendices/5-1-upgrade-guide.rst index da96d27aa6..4b0ddd8364 100644 --- a/en/appendices/5-1-upgrade-guide.rst +++ b/en/appendices/5-1-upgrade-guide.rst @@ -32,6 +32,12 @@ Console - ``Arguments::getBooleanOption()`` and ``Arguments::getMultipleOption()`` were added. +Core +---- + +- The ``toString``, ``toInt``, ``toBool`` functions were added. They give you + a typesafe way to cast request data or other input and return ``null`` when conversion fails. + Http ---- diff --git a/en/controllers/request-response.rst b/en/controllers/request-response.rst index 47f1c5671c..c5ea1f9b7e 100644 --- a/en/controllers/request-response.rst +++ b/en/controllers/request-response.rst @@ -100,6 +100,25 @@ If you want to access all the query parameters you can use $query = $this->request->getQueryParams(); +You can use the casting utility functions to provide typesafe access to request +data and other input:: + + use function Cake\Core\toBool; + use function Cake\Core\toInt; + use function Cake\Core\toString; + + // $active is bool|null. + $active = toBool($this->request->getQuery('active')); + + // $page is int|null. + $page = toInt($this->request->getQuery('page')); + + // $query is string|null. + $query = toString($this->request->getQuery('query')); + +.. versionadded:: 5.1.0 + Casting functions were added. + Request Body Data -----------------