Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions en/controllers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -142,31 +142,6 @@ If for some reason you'd like to skip the default behavior, you can return a
:php:class:`Cake\\Network\\Response` object from the action with the fully
created response.

When you use controller methods with
:php:meth:`~Cake\\Routing\\RequestActionTrait::requestAction()`
you will typically return a ``Response`` instance. If you have controller
methods that are used for normal web requests + requestAction, you should check
the request type before returning::

// src/Controller/RecipesController.php

class RecipesController extends AppController
{
public function popular()
{
$popular = $this->Recipes->find('popular');
if ($this->request->is('requested')) {
$this->response->body(json_encode($popular));
return $this->response;
}
$this->set('popular', $popular);
}
}

The above controller action is an example of how a method can be used with
:php:meth:`~Cake\\Routing\\RequestActionTrait::requestAction()` and normal
requests.

In order for you to use a controller effectively in your own application, we'll
cover some of the core attributes and methods provided by CakePHP's controllers.

Expand Down
5 changes: 0 additions & 5 deletions en/controllers/request-response.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ are also all found in the request parameters:
* ``action`` The action handling the current request.
* ``prefix`` The prefix for the current action. See :ref:`prefix-routing` for
more information.
* ``bare`` Present when the request came from
:php:meth:`~Cake\\Controller\\Controller::requestAction()` and included the
bare option. Bare requests do not have layouts rendered.
* ``requested`` Present and set to ``true`` when the action came from
:php:meth:`~Cake\\Controller\\Controller::requestAction()`.

Query String Parameters
-----------------------
Expand Down
106 changes: 2 additions & 104 deletions en/development/routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,7 @@ CakePHP, and should not be used unless you want the special meaning
* ``_ext`` Used for :ref:`File extentions routing <file-extensions>`.
* ``_base`` Set to ``false`` to remove the base path from the generated URL. If
your application is not in the root directory, this can be used to generate
URLs that are 'cake relative'. cake relative URLs are required when using
requestAction.
URLs that are 'cake relative'.
* ``_scheme`` Set to create links on different schemes like `webcal` or `ftp`.
Defaults to the current scheme.
* ``_host`` Set the host to use for the link. Defaults to the current host.
Expand Down Expand Up @@ -977,8 +976,7 @@ You can also use any of the special route elements when generating URLs:
* ``_ext`` Used for :ref:`file-extensions` routing.
* ``_base`` Set to ``false`` to remove the base path from the generated URL. If
your application is not in the root directory, this can be used to generate
URLs that are 'cake relative'. cake relative URLs are required when using
requestAction.
URLs that are 'cake relative'.
* ``_scheme`` Set to create links on different schemes like ``webcal`` or
``ftp``. Defaults to the current scheme.
* ``_host`` Set the host to use for the link. Defaults to the current host.
Expand Down Expand Up @@ -1170,106 +1168,6 @@ This will populate ``$this->request->params['named']`` with any named parameters
found in the passed arguments. Any passed argument that was interpreted as a
named parameter, will be removed from the list of passed arguments.

.. _request-action:

RequestActionTrait
==================

.. php:trait:: RequestActionTrait

This trait allows classes which include it to create sub-requests or
request actions.

.. php:method:: requestAction(string $url, array $options)

This function calls a controller's action from any location and
returns the response body from the action. The ``$url`` passed is a
CakePHP-relative URL (/controllername/actionname/params). To pass
extra data to the receiving controller action add to the $options
array.

.. note::

You can use ``requestAction()`` to retrieve a rendered view by passing
'return' in the options: ``requestAction($url, ['return']);``. It is
important to note that making a requestAction using 'return' from
a controller method may cause script and css tags to not work correctly.

Generally you can avoid dispatching sub-requests by using
:doc:`/views/cells`. Cells give you a lightweight way to create re-usable
view components when compared to ``requestAction()``.

You should always include checks to make sure your requestAction methods are
actually originating from ``requestAction()``. Failing to do so will allow
requestAction methods to be directly accessible from a URL, which is
generally undesirable.

If we now create a simple element to call that function::

// src/View/Element/latest_comments.ctp
echo $this->requestAction('/comments/latest');

We can then place that element anywhere to get the output
using::

echo $this->element('latest_comments');

Written in this way, whenever the element is rendered, a request will be
made to the controller to get the data, the data will be processed, rendered
and returned. However in accordance with the warning above it's best to make
use of element caching to prevent needless processing. By modifying the call
to element to look like this::

echo $this->element('latest_comments', [], ['cache' => '+1 hour']);

The ``requestAction`` call will not be made while the cached
element view file exists and is valid.

In addition, requestAction takes routing array URLs::

echo $this->requestAction(
['controller' => 'Articles', 'action' => 'featured']
);

.. note::

Unlike other places where array URLs are analogous to string URLs,
requestAction treats them differently.

The URL based array are the same as the ones that
:php:meth:`Cake\\Routing\\Router::url()` uses with one difference - if you
are using passed parameters, you must put them in a second array and wrap
them with the correct key. This is because requestAction merges the extra
parameters (requestAction's 2nd parameter) with the ``request->params``
member array and does not explicitly place them under the ``pass`` key. Any
additional keys in the ``$option`` array will be made available in the
requested action's ``request->params`` property::

echo $this->requestAction('/articles/view/5');

As an array in the requestAction would then be::

echo $this->requestAction(
['controller' => 'Articles', 'action' => 'view'],
['pass' => [5]]
);

You can also pass querystring arguments, post data or cookies using the
appropriate keys. Cookies can be passed using the ``cookies`` key.
Get parameters can be set with ``query`` and post data can be sent
using the ``post`` key::

$vars = $this->requestAction('/articles/popular', [
'query' => ['page' = > 1],
'cookies' => ['remember_me' => 1],
]);

When using an array URL in conjunction with requestAction() you
must specify **all** parameters that you will need in the requested
action. This includes parameters like ``$this->request->data``. In addition
to passing all required parameters, passed arguments must be done
in the second array as seen above.

.. toctree::
:glob:
:maxdepth: 1
Expand Down
7 changes: 2 additions & 5 deletions en/views/cells.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ View Cells
##########

View cells are small mini-controllers that can invoke view logic and render out
templates. They provide a light-weight modular replacement to
``requestAction()``. The idea of cells is borrowed from `cells in Ruby
templates. The idea of cells is borrowed from `cells in Ruby
<https://github.com/apotonick/cells>`_, where they fulfill a similar role and
purpose.

Expand All @@ -12,9 +11,7 @@ When to use Cells

Cells are ideal for building reusable page components that require interaction
with models, view logic, and rendering logic. A simple example would be the
cart in an online store, or a data-driven navigation menu in a CMS. Because
cells do not dispatch sub-requests, they sidestep all of the overhead associated
with ``requestAction()``.
cart in an online store, or a data-driven navigation menu in a CMS.

Creating a Cell
===============
Expand Down