本文整理汇总了PHP中Cake\Routing\DispatcherFactory::create方法的典型用法代码示例。如果您正苦于以下问题:PHP DispatcherFactory::create方法的具体用法?PHP DispatcherFactory::create怎么用?PHP DispatcherFactory::create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cake\Routing\DispatcherFactory
的用法示例。
在下文中一共展示了DispatcherFactory::create方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: execute
/**
* Run a request and get the response.
*
* @param array $request The request context to execute.
* @return \Cake\Network\Response The generated response.
*/
public function execute($request)
{
$request = new Request($request);
$response = new Response();
$dispatcher = DispatcherFactory::create();
$dispatcher->eventManager()->on('Dispatcher.invokeController', ['priority' => 999], [$this->_test, 'controllerSpy']);
$dispatcher->dispatch($request, $response);
return $response;
}
示例2: index
/**
* Catch all for Opauth
* Handling callback
*/
public function index()
{
$this->_loadOpauth();
try {
$callback = ['validated' => true, 'response' => $this->Opauth->run()];
} catch (OpauthException $e) {
$callback = ['validated' => false, 'message' => $e->getMessage(), 'code' => $e->getCode()];
}
$Request = new Request(Configure::read('Opauth.CompleteURL'));
$Request->data = $callback;
$dispatcher = DispatcherFactory::create();
$dispatcher->dispatch($Request, new Response());
exit;
}
示例3: dirname
/**
* 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 = parse_url(urldecode($_SERVER['REQUEST_URI']));
$file = __DIR__ . $url['path'];
if (strpos($url['path'], '..') === false && strpos($url['path'], '.') !== false && is_file($file)) {
return false;
}
}
require dirname(__DIR__) . '/config/bootstrap.php';
use Cake\Network\Request;
use Cake\Network\Response;
use Cake\Routing\DispatcherFactory;
$dispatcher = DispatcherFactory::create();
$dispatcher->dispatch(Request::createFromGlobals(), new Response());
示例4: doRequest
/**
* Makes a request.
*
* @param \Cake\Network\Request $request Cake request.
* @return \Cake\Network\Response Cake response.
*/
protected function doRequest($request)
{
$response = new Response();
$dispatcher = DispatcherFactory::create();
$dispatcher->eventManager()->on('Dispatcher.beforeDispatch', ['priority' => 999], [$this, 'controllerSpy']);
try {
$dispatcher->dispatch($request, $response);
} catch (\PHPUnit_Exception $e) {
throw $e;
} catch (\Exception $e) {
$response = $this->handleError($e);
}
return $response;
}
示例5: _shutdown
/**
* Run the shutdown events.
*
* Triggers the afterFilter and afterDispatch events.
*
* @return \Cake\Network\Response The response to serve.
*/
protected function _shutdown()
{
$this->controller->dispatchEvent('Controller.shutdown');
$dispatcher = DispatcherFactory::create();
$args = ['request' => $this->controller->request, 'response' => $this->controller->response];
$result = $dispatcher->dispatchEvent('Dispatcher.afterDispatch', $args);
return $result->data['response'];
}
示例6: requestAction
//.........这里部分代码省略.........
* 'query' => ['page' => 1],
* 'cookies' => ['remember_me' => 1],
* ]);
* ```
*
* ### Sending environment or header values
*
* 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']
* ]);
* ```
*
* ### Transmitting the session
*
* 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)
* ]);
* ```
*
* @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.
* @deprecated 3.3.0 You should refactor your code to use View Cells instead of this method.
*/
public function requestAction($url, array $extra = [])
{
if (empty($url)) {
return false;
}
if (($index = array_search('return', $extra)) !== false) {
$extra['return'] = 0;
$extra['autoRender'] = 1;
unset($extra[$index]);
}
$extra += ['autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1];
$baseUrl = Configure::read('App.fullBaseUrl');
if (is_string($url) && strpos($url, $baseUrl) === 0) {
$url = Router::normalize(str_replace($baseUrl, '', $url));
}
if (is_string($url)) {
$params = ['url' => $url];
} elseif (is_array($url)) {
$defaultParams = ['plugin' => null, 'controller' => null, 'action' => null];
$params = ['params' => $url + $defaultParams, 'base' => false, 'url' => Router::reverse($url)];
if (empty($params['params']['pass'])) {
$params['params']['pass'] = [];
}
}
$current = Router::getRequest();
if ($current) {
$params['base'] = $current->base;
$params['webroot'] = $current->webroot;
}
$params['post'] = $params['query'] = [];
if (isset($extra['post'])) {
$params['post'] = $extra['post'];
}
if (isset($extra['query'])) {
$params['query'] = $extra['query'];
}
if (isset($extra['cookies'])) {
$params['cookies'] = $extra['cookies'];
}
if (isset($extra['environment'])) {
$params['environment'] = $extra['environment'] + $_SERVER + $_ENV;
}
unset($extra['environment'], $extra['post'], $extra['query']);
$params['session'] = isset($extra['session']) ? $extra['session'] : new Session();
$request = new Request($params);
$request->addParams($extra);
$dispatcher = DispatcherFactory::create();
// If an application is using PSR7 middleware,
// we need to 'fix' their missing dispatcher filters.
$needed = ['routing' => RoutingFilter::class, 'controller' => ControllerFactoryFilter::class];
foreach ($dispatcher->filters() as $filter) {
if ($filter instanceof RoutingFilter) {
unset($needed['routing']);
}
if ($filter instanceof ControllerFactoryFilter) {
unset($needed['controller']);
}
}
foreach ($needed as $class) {
$dispatcher->addFilter(new $class());
}
$result = $dispatcher->dispatch($request, new Response());
Router::popRequest();
return $result;
}
示例7: _sendRequest
/**
* Creates and send the request into a Dispatcher instance.
*
* Receives and stores the response for future inspection.
*
* @param string|array $url The URL
* @param string $method The HTTP method
* @param array|null $data The request data.
* @return void
* @throws \Exception
*/
protected function _sendRequest($url, $method, $data = [])
{
$request = $this->_buildRequest($url, $method, $data);
$response = new Response();
$dispatcher = DispatcherFactory::create();
$dispatcher->eventManager()->on('Dispatcher.beforeDispatch', ['priority' => 999], [$this, 'controllerSpy']);
try {
$dispatcher->dispatch($request, $response);
$this->_requestSession = $request->session();
$this->_response = $response;
} catch (PHPUnit_Exception $e) {
throw $e;
} catch (DatabaseException $e) {
throw $e;
} catch (Exception $e) {
$this->_exception = $e;
$this->_handleError($e);
}
}
示例8: requestAction
//.........这里部分代码省略.........
*
* 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 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],
* ]);
* ```
*
* ### Sending environment or header values
*
* 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']
* ]);
* ```
*
* ### Transmitting the session
*
* 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)
* ]);
* ```
*
* @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 = [])
{
if (empty($url)) {
return false;
}
if (($index = array_search('return', $extra)) !== false) {
$extra['return'] = 0;
$extra['autoRender'] = 1;
unset($extra[$index]);
}
$extra += ['autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1];
$baseUrl = Configure::read('App.fullBaseUrl');
if (is_string($url) && strpos($url, $baseUrl) === 0) {
$url = Router::normalize(str_replace($baseUrl, '', $url));
}
if (is_string($url)) {
$params = ['url' => $url];
} elseif (is_array($url)) {
$defaultParams = ['plugin' => null, 'controller' => null, 'action' => null];
$params = ['params' => $url + $defaultParams, 'base' => false, 'url' => Router::reverse($url)];
if (empty($params['params']['pass'])) {
$params['params']['pass'] = [];
}
}
$current = Router::getRequest();
if ($current) {
$params['base'] = $current->base;
$params['webroot'] = $current->webroot;
}
$params['post'] = $params['query'] = [];
if (isset($extra['post'])) {
$params['post'] = $extra['post'];
}
if (isset($extra['query'])) {
$params['query'] = $extra['query'];
}
if (isset($extra['cookies'])) {
$params['cookies'] = $extra['cookies'];
}
if (isset($extra['environment'])) {
$params['environment'] = $extra['environment'] + $_SERVER + $_ENV;
}
unset($extra['environment'], $extra['post'], $extra['query']);
$params['session'] = isset($extra['session']) ? $extra['session'] : new Session();
$request = new Request($params);
$request->addParams($extra);
$dispatcher = DispatcherFactory::create();
$result = $dispatcher->dispatch($request, new Response());
Router::popRequest();
return $result;
}
示例9: testCreate
/**
* Test creating a dispatcher with the factory
*
* @return void
*/
public function testCreate()
{
$mw = $this->getMock('Cake\\Routing\\DispatcherFilter', ['beforeDispatch']);
DispatcherFactory::add($mw);
$result = DispatcherFactory::create();
$this->assertInstanceOf('Cake\\Routing\\Dispatcher', $result);
$this->assertCount(1, $result->filters());
}
示例10: _shutdown
/**
* Run the shutdown events.
*
* Triggers the afterFilter and afterDispatch events.
*
* @return \Cake\Network\Response The response to serve.
*/
protected function _shutdown()
{
$this->controller->dispatchEvent('Controller.shutdown');
$dispatcher = DispatcherFactory::create();
$eventManager = $dispatcher->eventManager();
foreach ($dispatcher->filters() as $filter) {
$eventManager->attach($filter);
}
$args = ['request' => $this->controller->request, 'response' => $this->controller->response];
$result = $dispatcher->dispatchEvent('Dispatcher.afterDispatch', $args);
return $result->data['response'];
}
示例11: _testAction
/**
* Lets you do functional tests of a controller action.
*
* ### Options:
*
* - `data` The data to use for POST or PUT requests. If `method` is GET
* and `query` is empty, the data key will be used as GET parameters. By setting
* `data to a string you can simulate XML or JSON payloads allowing you to test
* REST webservices.
* - `query` The query string parameters to set.
* - `cookies` The cookie data to use for the request.
* - `method` POST or GET. Defaults to GET.
* - `return` Specify the return type you want. Choose from:
* - `vars` Get the set view variables.
* - `view` Get the rendered view, without a layout.
* - `contents` Get the rendered view including the layout.
* - `result` Get the return value of the controller action. Useful
* for testing requestAction methods.
*
* @param string $url The url to test
* @param array $options See options
* @return mixed
*/
protected function _testAction($url = '', $options = array())
{
$this->vars = $this->result = $this->view = $this->contents = $this->headers = null;
$this->_session = $this->_session ?: new Session();
$options += array('query' => array(), 'data' => array(), 'cookies' => array(), 'method' => 'GET', 'return' => 'result');
$method = strtoupper($options['method']);
$_SERVER['REQUEST_METHOD'] = $method;
if ($method === 'GET' && is_array($options['data']) && empty($options['query'])) {
$options['query'] = $options['data'];
$options['data'] = array();
}
$requestData = array('url' => $url, 'cookies' => $options['cookies'], 'query' => $options['query'], 'session' => $this->_session);
if (is_array($options['data'])) {
$requestData['post'] = $options['data'];
}
$request = $this->getMock('Cake\\Network\\Request', array('_readInput', 'method'), array($requestData));
$request->expects($this->any())->method('method')->will($this->returnValue($method));
if (is_string($options['data'])) {
$request->expects($this->any())->method('_readInput')->will($this->returnValue($options['data']));
}
if ($this->loadRoutes) {
Router::reload();
}
$request->addParams(Router::parse($request->url));
// Handle redirect routes.
if (!isset($request->params['controller']) && Router::getRequest()) {
$this->headers = Router::getRequest()->response->header();
return;
}
$stubFilter = new StubControllerFilter();
$dispatch = DispatcherFactory::create();
$dispatch->addFilter($stubFilter);
if ($this->_dirtyController) {
$this->controller = null;
}
if ($this->controller === null && $this->autoMock) {
$plugin = '';
if (!empty($request->params['plugin'])) {
$plugin = $request->params['plugin'] . '.';
}
$controllerName = $request->params['controller'];
if (!empty($request->params['prefix'])) {
$controllerName = Inflector::camelize($request->params['prefix']) . '/' . $controllerName;
}
$this->generate($plugin . $controllerName, [], $request);
}
$params = array();
if ($options['return'] === 'result') {
$params['return'] = 1;
$params['bare'] = 1;
$params['requested'] = 1;
}
$request->addParams($params);
$response = $this->getMock('Cake\\Network\\Response', array('send', 'stop'));
$stubFilter->response = $response;
$stubFilter->testController = $this->controller;
$this->result = $dispatch->dispatch($request, $response);
$this->controller = $stubFilter->testController;
$this->vars = $this->controller->viewVars;
$this->contents = $this->controller->response->body();
if (isset($this->controller->View)) {
$this->view = $this->controller->View->fetch('__view_no_layout__');
}
$this->_dirtyController = true;
$this->headers = $response->header();
return $this->{$options['return']};
}