RequestActionTrait.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. namespace Cake\Routing;
  15. use Cake\Core\Configure;
  16. use Cake\Network\Request;
  17. use Cake\Network\Response;
  18. use Cake\Routing\Dispatcher;
  19. use Cake\Routing\Router;
  20. /**
  21. * Provides the requestAction() method for doing sub-requests
  22. *
  23. */
  24. trait RequestActionTrait {
  25. /**
  26. * Calls a controller's method from any location. Can be used to connect controllers together
  27. * or tie plugins into a main application. requestAction can be used to return rendered views
  28. * or fetch the return value from controller actions.
  29. *
  30. * Under the hood this method uses Router::reverse() to convert the $url parameter into a string
  31. * URL. You should use URL formats that are compatible with Router::reverse()
  32. *
  33. * ### Examples
  34. *
  35. * A basic example getting the return value of the controller action:
  36. *
  37. * {{{
  38. * $variables = $this->requestAction('/articles/popular');
  39. * }}}
  40. *
  41. * A basic example of request action to fetch a rendered page without the layout.
  42. *
  43. * {{{
  44. * $viewHtml = $this->requestAction('/articles/popular', ['return']);
  45. * }}}
  46. *
  47. * You can also pass the URL as an array:
  48. *
  49. * {{{
  50. * $vars = $this->requestAction(['controller' => 'articles', 'action' => 'popular']);
  51. * }}}
  52. *
  53. * ### Passing other request data
  54. *
  55. * You can pass POST, GET, COOKIE and other data into the request using the apporpriate keys.
  56. * Cookies can be passed using the `cookies` key. Get parameters can be set with `query` and post
  57. * data can be sent using the `post` key.
  58. *
  59. * {{{
  60. * $vars = $this->requestAction('/articles/popular', [
  61. * 'query' => ['page' = > 1],
  62. * 'cookies' => ['remember_me' => 1],
  63. * ]);
  64. * }}}
  65. *
  66. * @param string|array $url String or array-based url. Unlike other url arrays in CakePHP, this
  67. * url will not automatically handle passed arguments in the $url parameter.
  68. * @param array $extra if array includes the key "return" it sets the autoRender to true. Can
  69. * also be used to submit GET/POST data, and passed arguments.
  70. * @return mixed Boolean true or false on success/failure, or contents
  71. * of rendered action if 'return' is set in $extra.
  72. */
  73. public function requestAction($url, array $extra = array()) {
  74. if (empty($url)) {
  75. return false;
  76. }
  77. if (($index = array_search('return', $extra)) !== false) {
  78. $extra['return'] = 0;
  79. $extra['autoRender'] = 1;
  80. unset($extra[$index]);
  81. }
  82. $extra = array_merge(
  83. ['autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1],
  84. $extra
  85. );
  86. $post = $query = [];
  87. if (isset($extra['post'])) {
  88. $post = $extra['post'];
  89. }
  90. if (isset($extra['query'])) {
  91. $query = $extra['query'];
  92. }
  93. unset($extra['post'], $extra['query']);
  94. if (is_string($url) && strpos($url, Configure::read('App.fullBaseUrl')) === 0) {
  95. $url = Router::normalize(str_replace(Configure::read('App.fullBaseUrl'), '', $url));
  96. }
  97. if (is_string($url)) {
  98. $params = [
  99. 'url' => $url
  100. ];
  101. } elseif (is_array($url)) {
  102. $params = array_merge($url, [
  103. 'pass' => [],
  104. 'base' => false,
  105. 'url' => Router::reverse($url)
  106. ]);
  107. }
  108. if (!empty($post)) {
  109. $params['post'] = $post;
  110. }
  111. if (!empty($query)) {
  112. $params['query'] = $query;
  113. }
  114. $request = new Request($params);
  115. $dispatcher = new Dispatcher();
  116. $result = $dispatcher->dispatch($request, new Response(), $extra);
  117. Router::popRequest();
  118. return $result;
  119. }
  120. }