use web\frontend\{View, Handler, Get};
#[Handler]
class Blog {
public function __construct(private Database $database) { }
#[Get]
public function index() {
return $this->database->newest();
}
#[Get('/{id}')]
public function article($id) {
$article= $this->database->article($id);
return View::named('article')->with($article)->cache('max-age=604800, must-revalidate');
}
}
The index handler will use a Cache-Control: no-cache (which is the default), while the article handler will overwrite this according to the recommendation from the Cache-Control issue:
For dynamic pages which are unfrequently updated (say, a blog), we can cache a little bit more using Cache-Control: max-age=604800, must-revalidate (quote: This tells the browser to cache the HTML page for one week (604,800 seconds), and once that week is up, we need to check with the server for updates.)
See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
Example
The index handler will use a
Cache-Control: no-cache
(which is the default), while the article handler will overwrite this according to the recommendation from the Cache-Control issue: