Dispatches a request based on a request object (an instance or subclass of
lithium\net\http\Request).
public static run ( object $request, array $options = [] ) : mixed | ||
$request | object | An instance of a request object (usually `lithium\action\Request`) with HTTP request information. |
$options | array | |
return | mixed | Returns the value returned from the callable object retrieved from `Dispatcher::_callable()`, which is either a string or an instance of `lithium\action\Response`. |
/** * Builds a composite AMF response based on the response bodies inside the * original AMF request. * * @return Zend_Amf_Response_Http */ public function processResponseBodies() { $responseBodies = $this->request->getAmfBodies(); foreach ($responseBodies as $body) { //Extract params from request body $return = $this->extractUriAndParams($body); //Create fake request object $liRequest = new Request(array('data' => $return['params'])); //Assign URL to request based on details if (isset($return['source'])) { $liRequest->url = '/' . $return['source'] . '/' . $return['method']; } elseif (isset($return['targetURI'])) { $liRequest->url = '/' . $return['targetURI']; } //Assign request params $liRequest->params += $return['params']; //Dispatch the request normally, and get the controller data $controllerResponse = Dispatcher::run($liRequest); //Add on the response data (or error) to the current response if (isset($controllerResponse->body['error'])) { $netStatusEvent = new StdClass(); $netStatusEvent->_explicitType = 'flex.messaging.messages.ErrorMessage'; $netStatusEvent->faultString = $controllerResponse->body['error']; $newBody = new \Zend_Amf_Value_MessageBody($body->getResponseURI() . \Zend_AMF_Constants::STATUS_METHOD, null, $netStatusEvent); $this->response->addAmfBody($newBody); } else { $newBody = new \Zend_Amf_Value_MessageBody($body->getResponseURI() . \Zend_AMF_Constants::STATUS_METHOD, null, $controllerResponse->body); $this->response->addAmfBody($newBody); } } return $this->response; }
public function testPluginControllerLookupFail() { Dispatcher::config(array('classes' => array('router' => __CLASS__))); $this->expectException("/Controller `some_invalid_plugin.Controller` not found/"); Dispatcher::run(new Request(array('url' => '/plugin'))); }
<?php require 'bootstrap.php'; use lithium\net\http\Router; use lithium\action\Dispatcher; use lithium\action\Response; Router::connect('/', array(), function ($request) { $body = '<h1>Welcome to Sinatrium</h1>'; return new Response(compact('body')); }); Router::connect('/hello/{:name}', array('name' => false), function ($request) { $name = ucwords($request->name) ?: 'World'; $body = "<h1>Hello {$name}!</h1>"; return new Response(compact('body')); }); echo Dispatcher::run(new lithium\action\Request());
* directory as your application. If you use the same libraries in multiple applications, you can * set this to a shared path on your server. */ define('LITHIUM_LIBRARY_PATH', dirname(__DIR__) . '/libraries'); /** * Locate and load Lithium core library files. Throws a fatal error if the core can't be found. * If your Lithium core directory is named something other than 'lithium', change the string below. */ if (!(include LITHIUM_LIBRARY_PATH . '/lithium/core/Libraries.php')) { $message = "Lithium core could not be found. Check the value of LITHIUM_LIBRARY_PATH in "; $message .= __FILE__ . ". It should point to the directory containing your "; $message .= "/libraries directory."; throw new ErrorException($message); } /** * Add Lithium */ Libraries::add('lithium'); /** * Include routes */ include 'routes.php'; /** * Include filters */ include 'filters.php'; /** * Run It! */ echo Dispatcher::run(new Request());