Caching
With "caching" you ensure that generated pages are reused and not regenerated on every request. This improves performance and reduces resource consumption. Fast loading times have a positive effect on "user experience" as well as SEO.
Applying Caching in Controllers
Use the methods ControllerAdapter::assignResponseCacheControl() and ControllerAdapter::assignHttpCacheControl() to control the Response and Http Cache for your controller methods. Read the Response Cache and Http Cache articles to learn more about this topic.
You should try to ensure that caching is active in some form for all of your pages. It is best to use Response and Http Cache together.
class ExampleController extends ControllerAdapter {
public function doFoo() {
$this->assignHttpCacheControl(new \DateInterval('PT30M'));
$this->assignResponseCacheControl(new \DateInterval('P1D'));
$this->forward('view\bar.html');
}
}
Cache Control
Cache Control properties that are assigned to the controller via ControllerAdapter::assignResponseCacheControl() or ControllerAdapter::assignHttpCacheControl() only work in combination with ControllerAdapter::forward..(), ControllerAdapter::redirect..() or ControllerAdapter::send(). If you do not use any of these methods and still want the response to be cached, you must pass the Cache Control properties directly to the response. For this you need the types n2n\web\http\ResponseCacheControl and n2n\web\http\HttpCacheControl.
public function doFoo() {
$this->getResponse()->setHttpCacheControl(
new HttpCacheControl(new \DateInterval('PT30M')));
$this->getResponse()->setResponseCacheControl(
new ResponseCacheControl(new \DateInterval('PT30M')));
$this->getResponse()->setHeader('Content-Type: text/plain; charset=utf-8');
echo 'text without view';
}
Modify the response header exclusively via n2n\web\http\Response and avoid PHP functions like header(). These could interfere with the exception handling.
CommentsQuestions
You must be logged in to be able to create posts.