/**
  * Executes this controller, and return an {@link SS_HTTPResponse} object with the result.
  * 
  * This method first does a few set-up activities:
  *  - Push this controller ont to the controller stack - 
  *    see {@link Controller::curr()} for information about this.
  *  - Call {@link init()}
  *  - Defer to {@link RequestHandler->handleRequest()} to determine which action
  *    should be executed
  * 
  * Note: $requestParams['executeForm'] support was removed, 
  * make the following change in your URLs: 
  * "/?executeForm=FooBar" -> "/FooBar" 
  * Also make sure "FooBar" is in the $allowed_actions of your controller class.
  * 
  * Note: You should rarely need to overload run() - 
  * this kind of change is only really appropriate for things like nested
  * controllers - {@link ModelAsController} and {@link RootURLController} 
  * are two examples here.  If you want to make more
  * orthodox functionality, it's better to overload {@link init()} or {@link index()}.
  * 
  * Important: If you are going to overload handleRequest, 
  * make sure that you start the method with $this->pushCurrent()
  * and end the method with $this->popCurrent().  
  * Failure to do this will create weird session errors.
  * 
  * @param $request The {@link SS_HTTPRequest} object that is responsible 
  *  for distributing request parsing.
  * @return SS_HTTPResponse The response that this controller produces, 
  *  including HTTP headers such as redirection info
  */
 public function handleRequest(SS_HTTPRequest $request, DataModel $model)
 {
     if (!$request) {
         user_error("Controller::handleRequest() not passed a request!", E_USER_ERROR);
     }
     $this->pushCurrent();
     $this->urlParams = $request->allParams();
     $this->request = $request;
     $this->response = new SS_HTTPResponse();
     $this->setDataModel($model);
     $this->extend('onBeforeInit');
     // Init
     $this->baseInitCalled = false;
     $this->init();
     if (!$this->baseInitCalled) {
         user_error("init() method on class '{$this->class}' doesn't call Controller::init()." . "Make sure that you have parent::init() included.", E_USER_WARNING);
     }
     $this->extend('onAfterInit');
     // If we had a redirection or something, halt processing.
     if ($this->response->isFinished()) {
         $this->popCurrent();
         return $this->response;
     }
     $body = parent::handleRequest($request, $model);
     if ($body instanceof SS_HTTPResponse) {
         if (isset($_REQUEST['debug_request'])) {
             Debug::message("Request handler returned SS_HTTPResponse object to {$this->class} controller;" . "returning it without modification.");
         }
         $this->response = $body;
     } else {
         if ($body instanceof Object && $body->hasMethod('getViewer')) {
             if (isset($_REQUEST['debug_request'])) {
                 Debug::message("Request handler {$body->class} object to {$this->class} controller;" . "rendering with template returned by {$body->class}::getViewer()");
             }
             $body = $body->getViewer($request->latestParam('Action'))->process($body);
         }
         $this->response->setBody($body);
     }
     ContentNegotiator::process($this->response);
     HTTP::add_cache_headers($this->response);
     $this->popCurrent();
     return $this->response;
 }
Example #2
0
	/**
	 * Handles HTTP requests.
	 * 
	 * If you are going to overload handleRequest, make sure that you start the method with $this->pushCurrent()
	 * and end the method with $this->popCurrent().  Failure to do this will create weird session errors.
	 * 
	 * @param $request The {@link HTTPRequest} object that is responsible for distributing request parsing.
	 */
	function handleRequest(HTTPRequest $request) {
		if(!$request) user_error("Controller::handleRequest() not passed a request!", E_USER_ERROR);
		
		$this->pushCurrent();
		$this->urlParams = $request->allParams();
		$this->request = $request;
		$this->response = new HTTPResponse();

		// Init
		$this->baseInitCalled = false;	
		$this->init();
		if(!$this->baseInitCalled) user_error("init() method on class '$this->class' doesn't call Controller::init().  Make sure that you have parent::init() included.", E_USER_WARNING);

		// If we had a redirection or something, halt processing.
		if($this->response->isFinished()) {
			$this->popCurrent();
			return $this->response;
		}

		$body = parent::handleRequest($request);
		if($body instanceof HTTPResponse) {
			if(isset($_REQUEST['debug_request'])) Debug::message("Request handler returned HTTPResponse object to $this->class controller; returning it without modification.");
			$this->response = $body;
			
		} else {
			if(is_object($body)) {
				if(isset($_REQUEST['debug_request'])) Debug::message("Request handler $body->class object to $this->class controller;, rendering with template returned by $body->class::getViewer()");
			   $body = $body->getViewer($request->latestParam('Action'))->process($body);
			}
			
			$this->response->setBody($body);
		}


		ContentNegotiator::process($this->response);
		HTTP::add_cache_headers($this->response);

		$this->popCurrent();
		return $this->response;
	}
 /**
  * Executes this controller, and return an {@link SS_HTTPResponse} object with the result.
  *
  * This method defers to {@link RequestHandler->handleRequest()} to determine which action
  *    should be executed
  *
  * Note: You should rarely need to overload handleRequest() -
  * this kind of change is only really appropriate for things like nested
  * controllers - {@link ModelAsController} and {@link RootURLController}
  * are two examples here.  If you want to make more
  * orthodox functionality, it's better to overload {@link init()} or {@link index()}.
  *
  * Important: If you are going to overload handleRequest,
  * make sure that you start the method with $this->beforeHandleRequest()
  * and end the method with $this->afterHandleRequest()
  *
  * @param SS_HTTPRequest $request
  * @param DataModel $model
  *
  * @return SS_HTTPResponse
  */
 public function handleRequest(SS_HTTPRequest $request, DataModel $model)
 {
     if (!$request) {
         user_error("Controller::handleRequest() not passed a request!", E_USER_ERROR);
     }
     //set up the controller for the incoming request
     $this->beforeHandleRequest($request, $model);
     //if the before handler manipulated the response in a way that we shouldn't proceed, then skip our request
     // handling
     if (!$this->getResponse()->isFinished()) {
         //retrieve the response for the request
         $response = parent::handleRequest($request, $model);
         //prepare the response (we can receive an assortment of response types (strings/objects/HTTPResponses)
         $this->prepareResponse($response);
     }
     //after request work
     $this->afterHandleRequest();
     //return the response
     return $this->getResponse();
 }