/** * Set cache headers and 304 not modify if needed. * * @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Response $response */ protected function setCacheHeaders($request, $response) { if (starts_with($request->getPathInfo(), ['/images'])) { $stat = stat(session()->pull('requestImagePath')); } else { if (($view = $response->getOriginalContent()) instanceof View) { $stat = stat($view->getPath()); } } if (isset($stat)) { $response->setCache(['etag' => md5("{$stat['ino']}|{$stat['mtime']}|{$stat['size']}"), 'public' => true]); $response->setExpires(Carbon::now()->addDays(30)); if (null !== ($etag = $request->headers->get('If-None-Match')) || null !== $request->headers->get('If-Modified-Since')) { $etags = explode('-', $etag, -1); $request->headers->set('If-None-Match', count($etags) ? $etags[0] . '"' : $etag); $response->isNotModified($request); } } }
/** * Call the callback with parameters. * * @param callable $callback * @param array $parameters * @param int $maxAge * @return \Symfony\Component\HttpFoundation\Response */ protected function call(callable $callback, array $parameters, $maxAge = 0) { $response = $this->app->call($callback, $parameters); if (!$response instanceof SymfonyResponse) { $response = new Response($response); } if ($maxAge) { $response->setCache(['private' => true, 'max_age' => $maxAge]); } return $response; }