/** * {@inheritdoc} * * @param string $module * @param string $controller * @param array $params * @return void */ protected function preDispatch($module, $controller, $params = array()) { // example of setup default title Layout::title("Bluz Skeleton"); // apply "remember me" function if (!AuthProxy::getIdentity()) { if ($token = Request::getHeader('Bluz-Token')) { Auth\Table::getInstance()->authenticateToken($token); } elseif (!empty($_COOKIE['rToken']) && !empty($_COOKIE['rId'])) { // try to login try { Auth\Table::getInstance()->authenticateCookie($_COOKIE['rId'], $_COOKIE['rToken']); } catch (AuthException $e) { $this->getResponse()->setCookie('rId', '', 1, '/'); $this->getResponse()->setCookie('rToken', '', 1, '/'); } } } parent::preDispatch($module, $controller, $params); }
/** * Method HEAD and GET * * @return mixed */ public function methodGet() { if (!empty($this->primary)) { // @throws NotFoundException $result = $this->readOne($this->primary); return [$result]; } else { // setup default offset and limit - safe way $offset = isset($this->params['offset']) ? $this->params['offset'] : 0; $limit = isset($this->params['limit']) ? $this->params['limit'] : 10; if ($range = Request::getHeader('Range')) { list(, $offset, $last) = preg_split('/[-=]/', $range); // for better compatibility $limit = $last - $offset; } return $this->readSet($offset, $limit, $this->params); } }
* @accept JSON * @method GET * * @param \Bluz\Crud\Table $crud * @param mixed $primary * @return array */ return function ($crud, $primary) { if (!empty($primary)) { // @throws NotFoundException return [$crud->readOne($primary)]; } else { $params = Request::getParams(); // setup default offset and limit - safe way $offset = Request::getParam('offset', 0); $limit = Request::getParam('limit', 10); if ($range = Request::getHeader('Range')) { list(, $offset, $last) = preg_split('/[-=]/', $range); // for better compatibility $limit = $last - $offset; } Response::setStatusCode(206); $total = 0; $result = $crud->readSet($offset, $limit, $params, $total); if (sizeof($result) < $total) { Response::setStatusCode(206); Response::setHeader('Content-Range', 'items ' . $offset . '-' . ($offset + sizeof($result)) . '/' . $total); } return $result; } };