/** * Create respective class instances for controller usage. * Mapped and re-assign to the core instances. */ protected function factory() { $this->request = $this->app->request(); $this->response = $this->app->response(); $this->route = $this->app->route(); $this->view = $this->app->view(); return $this; }
/** * Http caching with last modified by providing UNIX timestamp. * * @param mixed $timestamp * @return \Avenue\Response */ public function withLastModified($timestamp) { $this->withHeader(['Last-Modified' => $this->getGmtDateTime($timestamp)]); $httpIfModifiedSince = $this->app->request()->getHeader('If-Modified-Since'); if (strtotime($httpIfModifiedSince) === $timestamp) { $this->setCacheStatus(); } return $this; }
/** * Check against resource token in route's filter and, * overwrite the controller action based on the http request method * when resource token is true or targeted controller is matched. * * example of @resource token format: * * @resource => true (applied to any controllers that fulfilled the route mapping) * @resource => 'default' (only applied to 'DefaultController' class) * @resource => 'default|dummy' (only applied to 'DefaultController' and 'DummyController' classes) */ public function mapResourceMethod() { $delimiter = '|'; $controller = $this->getParams('controller'); $requestMethod = $this->app->request()->getRequestMethod(true); $resource = $this->filters['@resource']; $this->setParam('resource', $resource); // if true or with targeted controller if ($resource === true || !strpos($resource, $delimiter) && $resource === $controller) { $this->setParam('action', $requestMethod); return; } // if more than one controller if (strpos($resource, $delimiter) !== false) { $arrControllers = explode($delimiter, $resource); $filteredArrControllers = array_values(array_filter($arrControllers)); if (count($arrControllers) !== count($filteredArrControllers)) { throw new \InvalidArgumentException(sprintf('Invalid format of @resource token value [%s]!', $resource)); } if (in_array($controller, $arrControllers)) { $this->setParam('action', $requestMethod); } } }
public function testSingletonRequestClassInstanceViaStaticMethod() { $this->assertEquals($this->app->request(), App::request()); }