PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/cakephp/cakephp/src/Routing/RequestActionTrait.php

https://gitlab.com/vannh/portal_training
PHP | 157 lines | 64 code | 7 blank | 86 comment | 10 complexity | b1ec274b83947c2512472254fa060c4d MD5 | raw file
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) 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 (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Routing;
  15. use Cake\Core\Configure;
  16. use Cake\Network\Request;
  17. use Cake\Network\Response;
  18. use Cake\Network\Session;
  19. use Cake\Routing\DispatcherFactory;
  20. use Cake\Routing\Router;
  21. /**
  22. * Provides the requestAction() method for doing sub-requests
  23. *
  24. */
  25. trait RequestActionTrait
  26. {
  27. /**
  28. * Calls a controller's method from any location. Can be used to connect controllers together
  29. * or tie plugins into a main application. requestAction can be used to return rendered views
  30. * or fetch the return value from controller actions.
  31. *
  32. * Under the hood this method uses Router::reverse() to convert the $url parameter into a string
  33. * URL. You should use URL formats that are compatible with Router::reverse()
  34. *
  35. * ### Examples
  36. *
  37. * A basic example getting the return value of the controller action:
  38. *
  39. * ```
  40. * $variables = $this->requestAction('/articles/popular');
  41. * ```
  42. *
  43. * A basic example of request action to fetch a rendered page without the layout.
  44. *
  45. * ```
  46. * $viewHtml = $this->requestAction('/articles/popular', ['return']);
  47. * ```
  48. *
  49. * You can also pass the URL as an array:
  50. *
  51. * ```
  52. * $vars = $this->requestAction(['controller' => 'articles', 'action' => 'popular']);
  53. * ```
  54. *
  55. * ### Passing other request data
  56. *
  57. * You can pass POST, GET, COOKIE and other data into the request using the appropriate keys.
  58. * Cookies can be passed using the `cookies` key. Get parameters can be set with `query` and post
  59. * data can be sent using the `post` key.
  60. *
  61. * ```
  62. * $vars = $this->requestAction('/articles/popular', [
  63. * 'query' => ['page' => 1],
  64. * 'cookies' => ['remember_me' => 1],
  65. * ]);
  66. * ```
  67. *
  68. * ### Sending environment or header values
  69. *
  70. * By default actions dispatched with this method will use the global $_SERVER and $_ENV
  71. * values. If you want to override those values for a request action, you can specify the values:
  72. *
  73. * ```
  74. * $vars = $this->requestAction('/articles/popular', [
  75. * 'environment' => ['CONTENT_TYPE' => 'application/json']
  76. * ]);
  77. * ```
  78. *
  79. * ### Transmitting the session
  80. *
  81. * By default actions dispatched with this method will use the standard session object.
  82. * If you want a particular session instance to be used, you need to specify it.
  83. *
  84. * ```
  85. * $vars = $this->requestAction('/articles/popular', [
  86. * 'session' => new Session($someSessionConfig)
  87. * ]);
  88. * ```
  89. *
  90. * @param string|array $url String or array-based url. Unlike other url arrays in CakePHP, this
  91. * url will not automatically handle passed arguments in the $url parameter.
  92. * @param array $extra if array includes the key "return" it sets the autoRender to true. Can
  93. * also be used to submit GET/POST data, and passed arguments.
  94. * @return mixed Boolean true or false on success/failure, or contents
  95. * of rendered action if 'return' is set in $extra.
  96. */
  97. public function requestAction($url, array $extra = [])
  98. {
  99. if (empty($url)) {
  100. return false;
  101. }
  102. if (($index = array_search('return', $extra)) !== false) {
  103. $extra['return'] = 0;
  104. $extra['autoRender'] = 1;
  105. unset($extra[$index]);
  106. }
  107. $extra += ['autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1];
  108. $baseUrl = Configure::read('App.fullBaseUrl');
  109. if (is_string($url) && strpos($url, $baseUrl) === 0) {
  110. $url = Router::normalize(str_replace($baseUrl, '', $url));
  111. }
  112. if (is_string($url)) {
  113. $params = [
  114. 'url' => $url
  115. ];
  116. } elseif (is_array($url)) {
  117. $params = [
  118. 'params' => $url,
  119. 'base' => false,
  120. 'url' => Router::reverse($url)
  121. ];
  122. if (empty($params['params']['pass'])) {
  123. $params['params']['pass'] = [];
  124. }
  125. }
  126. $current = Router::getRequest();
  127. if ($current) {
  128. $params['base'] = $current->base;
  129. $params['webroot'] = $current->webroot;
  130. }
  131. $params['post'] = $params['query'] = [];
  132. if (isset($extra['post'])) {
  133. $params['post'] = $extra['post'];
  134. }
  135. if (isset($extra['query'])) {
  136. $params['query'] = $extra['query'];
  137. }
  138. if (isset($extra['environment'])) {
  139. $params['environment'] = $extra['environment'] + $_SERVER + $_ENV;
  140. }
  141. unset($extra['environment'], $extra['post'], $extra['query']);
  142. $params['session'] = isset($extra['session']) ? $extra['session'] : new Session();
  143. $request = new Request($params);
  144. $request->addParams($extra);
  145. $dispatcher = DispatcherFactory::create();
  146. $result = $dispatcher->dispatch($request, new Response());
  147. Router::popRequest();
  148. return $result;
  149. }
  150. }