Пример #1
0
 public function handle(IRequest $request)
 {
     if (strpos($request->getPath(), $this->pathPrefix) !== 0) {
         return null;
     }
     $params = $this->parsePath($request->getPath());
     // if no controller specified - use default controller.
     // This help distinct default controller and invalid controller
     $controllerName = isset($params[0]) ? array_shift($params) : $this->defaultController;
     $controller = $this->controllerFactory($this->namespace, $controllerName);
     if ($controller) {
         return $controller->handle($request, $params, $this->pathPrefix);
     } else {
         // no controller found, use default controller to handle 404 error
         $controller = $this->controllerFactory($this->namespace, $this->defaultController);
         return $controller->handleError(404, new RouteNotFoundException("Controller '{$controllerName}' not found"));
     }
 }
Пример #2
0
 public function handle(IRequest $request, array $params = array(), $prefix = '')
 {
     $this->request = $request;
     try {
         $action = isset($params[0]) ? array_shift($params) : $this->defaultAction;
         // translate some-name to SomeName
         $actionName = implode('', array_map('ucfirst', explode('-', $action)));
         $call = array($this, $actionName . $request->getMethod());
         if (is_callable($call)) {
             $res = $this->beforeHandle($action, $params);
             if ($res === null) {
                 $res = call_user_func_array($call, $params);
             }
         } else {
             $res = $this->handleError(404, new RouteNotFoundException("Action {$this->name}::{$actionName} not found"));
         }
     } catch (Exception $ex) {
         $res = $this->handleError(500, $ex);
         $action = '500';
     }
     return $this->actionResultToResponse($res, trim("{$prefix}/{$this->name}/{$action}.twig", '/'));
 }