Provides the requestAction() method for doing sub-requests
Calls a controller's method from any location. Can be used to connect controllers together or tie plugins into a main application. requestAction can be used to return rendered views or fetch the return value from controller actions.
requestAction( string|array $url , array $extra [] )
Calls a controller's method from any location. Can be used to connect controllers together or tie plugins into a main application. requestAction can be used to return rendered views or fetch the return value from controller actions.
Under the hood this method uses Router::reverse() to convert the $url parameter into a string URL. You should use URL formats that are compatible with Router::reverse()
A basic example getting the return value of the controller action:
$variables = $this->requestAction('/articles/popular');
A basic example of request action to fetch a rendered page without the layout.
$viewHtml = $this->requestAction('/articles/popular', ['return']);
You can also pass the URL as an array:
$vars = $this->requestAction(['controller' => 'articles', 'action' => 'popular']);
You can pass POST, GET, COOKIE and other data into the request 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], ]);
By default actions dispatched with this method will use the global $_SERVER and $_ENV values. If you want to override those values for a request action, you can specify the values:
$vars = $this->requestAction('/articles/popular', [ 'environment' => ['CONTENT_TYPE' => 'application/json'] ]);
By default actions dispatched with this method will use the standard session object. If you want a particular session instance to be used, you need to specify it.
$vars = $this->requestAction('/articles/popular', [ 'session' => new Session($someSessionConfig) ]);
$url
String or array-based url. Unlike other url arrays in CakePHP, this url will not automatically handle passed arguments in the $url parameter.
$extra
optional [] if array includes the key "return" it sets the autoRender to true. Can also be used to submit GET/POST data, and passed arguments.
Boolean true or false on success/failure, or contents of rendered action if 'return' is set in $extra.
© 2005–2017 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/3.4/class-Cake.Routing.RequestActionTrait.html