예제 #1
1
 /**
  * testHttpMethodOverrides method
  *
  * @return void
  */
 public function testHttpMethodOverrides()
 {
     Router::reload();
     Router::mapResources('Posts');
     $dispatcher = new Dispatcher();
     $request = new Request(['url' => '/posts', 'environment' => ['REQUEST_METHOD' => 'POST']]);
     $event = new Event(__CLASS__, $dispatcher, array('request' => $request));
     $dispatcher->parseParams($event);
     $expected = array('pass' => [], 'plugin' => null, 'controller' => 'posts', 'action' => 'add', '[method]' => 'POST');
     foreach ($expected as $key => $value) {
         $this->assertEquals($value, $request[$key], 'Value mismatch for ' . $key . ' %s');
     }
     $request = new Request(['url' => '/posts/5', 'environment' => ['REQUEST_METHOD' => 'GET', 'HTTP_X_HTTP_METHOD_OVERRIDE' => 'PUT']]);
     $event = new Event(__CLASS__, $dispatcher, array('request' => $request));
     $dispatcher->parseParams($event);
     $expected = array('pass' => array('5'), 'id' => '5', 'plugin' => null, 'controller' => 'posts', 'action' => 'edit', '[method]' => 'PUT');
     foreach ($expected as $key => $value) {
         $this->assertEquals($value, $request[$key], 'Value mismatch for ' . $key . ' %s');
     }
     $request = new Request(['url' => '/posts/5', 'environment' => ['REQUEST_METHOD' => 'GET']]);
     $event = new Event(__CLASS__, $dispatcher, array('request' => $request));
     $dispatcher->parseParams($event);
     $expected = array('pass' => array('5'), 'id' => '5', 'plugin' => null, 'controller' => 'posts', 'action' => 'view', '[method]' => 'GET');
     foreach ($expected as $key => $value) {
         $this->assertEquals($value, $request[$key], 'Value mismatch for ' . $key . ' %s');
     }
     $request = new Request(['url' => '/posts/5', 'post' => array('_method' => 'PUT')]);
     $event = new Event(__CLASS__, $dispatcher, array('request' => $request));
     $dispatcher->parseParams($event);
     $expected = array('pass' => array('5'), 'id' => '5', 'plugin' => null, 'controller' => 'posts', 'action' => 'edit', '[method]' => 'PUT');
     foreach ($expected as $key => $value) {
         $this->assertEquals($value, $request[$key], 'Value mismatch for ' . $key . ' %s');
     }
     $request = new Request(array('url' => '/posts', 'post' => array('_method' => 'POST', 'Post' => array('title' => 'New Post'), 'extra' => 'data')));
     $event = new Event(__CLASS__, $dispatcher, array('request' => $request));
     $dispatcher->parseParams($event);
     $expected = array('pass' => [], 'plugin' => null, 'controller' => 'posts', 'action' => 'add', '[method]' => 'POST', 'data' => array('extra' => 'data', 'Post' => array('title' => 'New Post')));
     foreach ($expected as $key => $value) {
         $this->assertEquals($value, $request[$key], 'Value mismatch for ' . $key . ' %s');
     }
 }
예제 #2
0
 /**
  * Create a dispatcher that has all the configured middleware applied.
  *
  * @return \Cake\Routing\Dispatcher
  */
 public static function create()
 {
     $dispatcher = new Dispatcher();
     foreach (static::$_stack as $middleware) {
         $dispatcher->addFilter($middleware);
     }
     return $dispatcher;
 }
 /**
  * Loads routes and resets if the test case dictates it should
  *
  * @return void
  */
 protected function _loadRoutes()
 {
     parent::_loadRoutes();
     if (!$this->loadRoutes) {
         Router::reload();
     }
 }
예제 #4
0
 /**
  * invoke method
  *
  * @param \Cake\Controller\Controller $controller
  * @return \Cake\Network\Response $response
  */
 protected function _invoke(Controller $controller)
 {
     $this->controller = $controller;
     return parent::_invoke($controller);
 }
예제 #5
0
/**
 * The Front Controller for handling every request
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @since         0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
// for built-in server
if (php_sapi_name() === 'cli-server') {
    $_SERVER['PHP_SELF'] = '/' . basename(__FILE__);
    $url = urldecode($_SERVER['REQUEST_URI']);
    $file = __DIR__ . $url;
    if (strpos($url, '..') === false && strpos($url, '.') !== false && is_file($file)) {
        return false;
    }
}
require dirname(__DIR__) . '/App/Config/bootstrap.php';
use Cake\Network\Request;
use Cake\Network\Response;
use Cake\Routing\Dispatcher;
$Dispatcher = new Dispatcher();
$Dispatcher->dispatch(Request::createFromGlobals(), new Response());
 /**
  * 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()
  *
  * ### Examples
  *
  * 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']);
  * }}}
  *
  * ### Passing other request data
  *
  * You can pass POST, GET, COOKIE and other data into the request using the apporpriate 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],
  * ]);
  * }}}
  *
  * @param string|array $url String or array-based url.  Unlike other url arrays in CakePHP, this
  *    url will not automatically handle passed arguments in the $url parameter.
  * @param array $extra if array includes the key "return" it sets the autoRender to true.  Can
  *    also be used to submit GET/POST data, and passed arguments.
  * @return mixed Boolean true or false on success/failure, or contents
  *    of rendered action if 'return' is set in $extra.
  */
 public function requestAction($url, array $extra = array())
 {
     if (empty($url)) {
         return false;
     }
     if (($index = array_search('return', $extra)) !== false) {
         $extra['return'] = 0;
         $extra['autoRender'] = 1;
         unset($extra[$index]);
     }
     $extra = array_merge(['autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1], $extra);
     $post = $query = [];
     if (isset($extra['post'])) {
         $post = $extra['post'];
     }
     if (isset($extra['query'])) {
         $query = $extra['query'];
     }
     unset($extra['post'], $extra['query']);
     if (is_string($url) && strpos($url, Configure::read('App.fullBaseUrl')) === 0) {
         $url = Router::normalize(str_replace(Configure::read('App.fullBaseUrl'), '', $url));
     }
     if (is_string($url)) {
         $params = ['url' => $url];
     } elseif (is_array($url)) {
         $params = array_merge($url, ['pass' => [], 'base' => false, 'url' => Router::reverse($url)]);
     }
     if (!empty($post)) {
         $params['post'] = $post;
     }
     if (!empty($query)) {
         $params['query'] = $query;
     }
     $request = new Request($params);
     $dispatcher = new Dispatcher();
     $result = $dispatcher->dispatch($request, new Response(), $extra);
     Router::popRequest();
     return $result;
 }