public function handleRequest(SS_HTTPRequest $request, DataModel $model)
 {
     // If this is the final portion of the request (i.e. the URL is just /admin), direct to the default panel
     if ($request->allParsed()) {
         $base = $this->config()->url_base;
         $segment = Config::inst()->get($this->config()->default_panel, 'url_segment');
         $this->redirect(Controller::join_links($base, $segment));
         return $this->getResponse();
     } else {
         $rules = self::rules();
         foreach ($rules as $pattern => $controller) {
             if (($arguments = $request->match($pattern, true)) !== false) {
                 $controllerObj = Injector::inst()->create($controller);
                 $controllerObj->setSession($this->session);
                 return $controllerObj->handleRequest($request, $model);
             }
         }
     }
     return $this->httpError(404, 'Not found');
 }
 function routeRequest(SS_HTTPRequest $request)
 {
     // Handle the routing
     $noun = singleton('RESTRoot');
     while (!$request->allParsed()) {
         $matched = false;
         if ($params = $request->match('$Next!', true)) {
             $matched = true;
             $next = $params['Next'];
             try {
                 if (method_exists($noun, 'getItem')) {
                     $noun = $noun->getItem($next);
                 } else {
                     $noun = $noun->{$next};
                 }
             } catch (Exception $e) {
                 if ($e instanceof SS_HTTPResponse_Exception) {
                     throw $e;
                 } elseif ($e instanceof RESTException) {
                     $handler = $this->getHandler($noun);
                     $handler->respondWithError(array('code' => $e->getCode(), 'exception' => $e));
                 } else {
                     $handler = $this->getHandler($noun);
                     $handler->respondWithError(array('code' => 500, 'exception' => $e));
                 }
             }
             if (!$noun) {
                 $this->httpError(404);
             }
         }
         if (!$matched) {
             $this->httpError(404);
         }
     }
     // Find the handler and call
     $handler = $this->getHandler($noun);
     return $handler->handleRequest($request);
 }
 /**
  *	Handle the current URL, parsing a year/month/day/media format, and directing towards any valid controller actions that may be defined.
  *
  *	@URLparameter <{YEAR}> integer
  *	@URLparameter <{MONTH}> integer
  *	@URLparameter <{DAY}> integer
  *	@URLparameter <{MEDIA_URL_SEGMENT}> string
  *	@return ss http response
  */
 public function handleURL()
 {
     // Retrieve the formatted URL.
     $request = $this->getRequest();
     $URL = $request->param('URL');
     // Determine whether a controller action resolves.
     if ($this->hasAction($URL) && $this->checkAccessAction($URL)) {
         $output = $this->{$URL}($request);
         // The current request URL has been successfully parsed.
         while (!$request->allParsed()) {
             $request->shift();
         }
         return $output;
     } else {
         if (!is_numeric($URL)) {
             // Determine whether a media page child once existed, and redirect appropriately.
             $response = $this->resolveURL();
             if ($response) {
                 // The current request URL has been successfully parsed.
                 while (!$request->allParsed()) {
                     $request->shift();
                 }
                 return $response;
             } else {
                 // The URL doesn't resolve.
                 return $this->httpError(404);
             }
         }
     }
     // Determine the formatted URL segments.
     $segments = array($URL);
     $remaining = $request->remaining();
     if ($remaining) {
         $remaining = explode('/', $remaining);
         // Determine the media page child to display.
         $child = null;
         $action = null;
         // Iterate the formatted URL segments.
         $iteration = 1;
         foreach ($remaining as $segment) {
             if (is_null($action)) {
                 // Update the current request.
                 $request->shift();
                 if ($child) {
                     // Determine whether a controller action has been defined.
                     $action = $segment;
                     break;
                 } else {
                     if (!is_numeric($segment)) {
                         if ($iteration === 4) {
                             // The remaining URL doesn't match the month/day/media format.
                             return $this->httpError(404);
                         }
                         // Determine the media page child to display, using the URL segment and date.
                         $children = MediaPage::get()->filter(array('ParentID' => $this->data()->ID, 'URLSegment' => $segment));
                         if (!empty($segments)) {
                             // Apply a partial match against the date, since the previous URL segments may only contain the year/month.
                             $date = array();
                             foreach ($segments as $previous) {
                                 $date[] = str_pad($previous, 2, '0', STR_PAD_LEFT);
                             }
                             $children = $children->filter(array('Date:StartsWith' => implode('-', $date)));
                         }
                         $child = $children->first();
                         // Determine whether a media page child once existed, and redirect appropriately.
                         if (is_null($child)) {
                             $response = $this->resolveURL();
                             if ($response) {
                                 // The current request URL has been successfully parsed.
                                 while (!$request->allParsed()) {
                                     $request->shift();
                                 }
                                 return $response;
                             } else {
                                 // The URL doesn't match the month/day/media format.
                                 return $this->httpError(404);
                             }
                         }
                     }
                 }
             }
             $segments[] = $segment;
             $iteration++;
         }
         // Retrieve the media page child controller, and determine whether an action resolves.
         if ($child) {
             $controller = ModelAsController::controller_for($child);
             // Determine whether a controller action resolves.
             if (is_null($action)) {
                 return $controller;
             } else {
                 if ($controller->hasAction($action) && $controller->checkAccessAction($action)) {
                     $output = $controller->{$action}($request);
                     // The current request URL has been successfully parsed.
                     while (!$request->allParsed()) {
                         $request->shift();
                     }
                     return $output;
                 } else {
                     // The controller action doesn't resolve.
                     return $this->httpError(404);
                 }
             }
         }
     }
     // Retrieve the paginated children using the date filter segments.
     $request = new SS_HTTPRequest('GET', $this->Link(), array_merge($request->getVars(), array('from' => implode('-', $segments))));
     // The new request URL doesn't require parsing.
     while (!$request->allParsed()) {
         $request->shift();
     }
     // Handle the new request URL.
     return $this->handleRequest($request);
 }