src/Mvc/Application.php
public function serve($applicationName = 'default', $serverVars = array(), Request $request = null, $send = true)
    {
        if (null === $request) {
            $request = Request::parseFromServerRequest($serverVars, $this->defaultController, $this->defaultAction);
        }
        
        foreach ($this->overridenClientHeaders as $headerName => $headerValue) {
            $request->setParam($headerName, $headerValue);
        }
        
        $controller = $request->getController();
        $action = $request->getAction();
        
        $this->getLogger()->debug("[{remote}] Requested controller is {controller} and action is {action}", array(
            'remote' => $request->getRemoteHost(),
            'controller' => $controller,
            'action' => $action
        ));
        
        if ( null != $this->router && $this->router->hasRoute($action) ) {
        	$controllerInstance = $this->router->route($action, $request);
        	$action = $request->getAction();
        }
        else {
	        if (! isset($this->controllers[$applicationName][$controller])) {
	            $this->getLogger()->error("[{remote}] No such controller {controller}", array(
	                'remote' => $request->getRemoteHost(),
	                'controller' => $controller
	            ));
	            $controller = 'Error';
	            $action = 'error';
	        }
	        
	        $controllerInstance = $this->controllers[$applicationName][$controller];
	        assert($controllerInstance instanceof AbstractController);
	        if (! $controllerInstance->hasAction($action)) {
	            $this->getLogger()->error("[{remote}] No such action {action}", array(
	                'remote' => $request->getRemoteHost(),
	                'action' => $action
	            ));
	            $controllerInstance = $this->controllers[$applicationName]['Error'];
	            $action = 'error';
	        }
	        
	        $this->getLogger()->debug("[{remote}] Routing request to {controller}:{action}", array(
	            'remote' => $request->getRemoteHost(),
	            'controller' => $controller,
	            'action' => $action
	        ));
        }
        
        $view = $this->getViewBestMatch($request, $applicationName);
        
        $view->setCssFiles($this->cssFiles);
        $view->setJsFiles($this->jsFiles);
        
        foreach ($this->viewControls as $controlIdentifier => $controlClass) {
            $view->registerControl($controlClass, $controlIdentifier);
        }
        
        try {
            $response = $controllerInstance->call($action, $request, $view);
        } catch (\Exception $ex) {
            $controllerInstance = $this->controllers[$applicationName]['Error'];
            $action = 'exception';
            $request->setException($ex);
            $response = $controllerInstance->call($action, $request, $view);
            
            $outputBuffer = ob_get_clean();
            if (strlen($outputBuffer)) {
                $response->appendBody($outputBuffer);
            }
        }
        
        $responseCode = $response->getHttpCode();
        $responseType = sprintf('%s;%s', $response->getType(), $response->getEncoding());
        $responseContent = strval($response);
        $responseLen = strlen($responseContent);
        
        $this->getLogger()->debug("[{remote}] Response is type of {type}, length of {length} and code {code}", array(
            'remote' => $request->getRemoteHost(),
            'type' => $responseType,
            'length' => $responseLen,
            'code' => $responseCode
        ));
        
        if ($send) {
            header(sprintf("%s", $responseCode));
            header(sprintf("Content-Length: %d", $responseLen));
            header(sprintf("Content-Type: %s", $responseType));
            
            foreach ($this->defaultHeaders as $headerName => $headerValue) {
                header(sprintf("%s: %s", $headerName, $headerValue));
            }
            foreach ($response->getAdditionalHeaders() as $headerName => $headerValue) {
                header(sprintf("%s: %s", $headerName, $headerValue));
            }
            
            echo $responseContent;
        }
        
        return $response;
    }
public function serve($applicationName = 'default', $serverVars = array(), Request $request = null, $send = true)
    {
        if (null === $request) {
            $request = Request::parseFromServerRequest($serverVars, $this->defaultController, $this->defaultAction);
        }
        
        foreach ($this->overridenClientHeaders as $headerName => $headerValue) {
            $request->setParam($headerName, $headerValue);
        }
        
        $controller = $request->getController();
        $action = $request->getAction();
        
        $this->getLogger()->debug("[{remote}] Requested controller is {controller} and action is {action}", array(
            'remote' => $request->getRemoteHost(),
            'controller' => $controller,
            'action' => $action
        ));
        
        if ( null != $this->router && $this->router->hasRoute($action) ) {
        	$controllerInstance = $this->router->route($action, $request);
        	$action = $request->getAction();
        }
        else {
	        if (! isset($this->controllers[$applicationName][$controller])) {
	            $this->getLogger()->error("[{remote}] No such controller {controller}", array(
	                'remote' => $request->getRemoteHost(),
	                'controller' => $controller
	            ));
	            $controller = 'Error';
	            $action = 'error';
	        }
	        
	        $controllerInstance = $this->controllers[$applicationName][$controller];
	        assert($controllerInstance instanceof AbstractController);
	        if (! $controllerInstance->hasAction($action)) {
	            $this->getLogger()->error("[{remote}] No such action {action}", array(
	                'remote' => $request->getRemoteHost(),
	                'action' => $action
	            ));
	            $controllerInstance = $this->controllers[$applicationName]['Error'];
	            $action = 'error';
	        }
	        
	        $this->getLogger()->debug("[{remote}] Routing request to {controller}:{action}", array(
	            'remote' => $request->getRemoteHost(),
	            'controller' => $controller,
	            'action' => $action
	        ));
        }
        
        $view = $this->getViewBestMatch($request, $applicationName);
        
        $view->setCssFiles($this->cssFiles);
        $view->setJsFiles($this->jsFiles);
        
        foreach ($this->viewControls as $controlIdentifier => $controlClass) {
            $view->registerControl($controlClass, $controlIdentifier);
        }
        
        try {
            $response = $controllerInstance->call($action, $request, $view);
        } catch (\Exception $ex) {
            $controllerInstance = $this->controllers[$applicationName]['Error'];
            $action = 'exception';
            $request->setException($ex);
            $response = $controllerInstance->call($action, $request, $view);
            
            $outputBuffer = ob_get_clean();
            if (strlen($outputBuffer)) {
                $response->appendBody($outputBuffer);
            }
        }
        
        $responseCode = $response->getHttpCode();
        $responseType = sprintf('%s;%s', $response->getType(), $response->getEncoding());
        $responseContent = strval($response);
        $responseLen = strlen($responseContent);
        
        $this->getLogger()->debug("[{remote}] Response is type of {type}, length of {length} and code {code}", array(
            'remote' => $request->getRemoteHost(),
            'type' => $responseType,
            'length' => $responseLen,
            'code' => $responseCode
        ));
        
        if ($send) {
            header(sprintf("%s", $responseCode));
            header(sprintf("Content-Length: %d", $responseLen));
            header(sprintf("Content-Type: %s", $responseType));
            
            foreach ($this->defaultHeaders as $headerName => $headerValue) {
                header(sprintf("%s: %s", $headerName, $headerValue));
            }
            foreach ($response->getAdditionalHeaders() as $headerName => $headerValue) {
                header(sprintf("%s: %s", $headerName, $headerValue));
            }
            
            echo $responseContent;
        }
        
        return $response;
    }
Start the application

@param string $applicationName
Optional application name to service the request for
@param array $serverVars
The server variables provided by sapi
@param Request $request
Optional previous generated request object
@param boolean $send
Optional whether to send the output directly to client

@throws ControllerException
@throws InvalidUrlException