Laraship Full Page Caching
Introduction
When Serving static URL in Laraship, like Pages, Blogs,.. and even some routes that are user-independent, it’s possible to implement full page caching where the rendered page is stored in the server as HTML and served instead of rendering the PHP code every time the page requested.
That said, for truly static pages on a site, there really is no reason to have to boot up a full Laravel core and application just to serve a static page. Serving a simple HTML page from disk is infinitely faster and less taxing on the server.
So the solution is to implement Full-page caching for instantly loading such pages.
We will demonstrate how to cache public requests for Laraship such as pages, blogs, posts and any selective route you need.
Installation
Install the page-cache
package with composer:
$ composer require silber/page-cache
Middleware
protected $routeMiddleware = [
'page-cache' => \Silber\PageCache\Middleware\CacheResponse::class,
/* ... keep the existing mappings here */
];
URL rewriting
In order to serve the static files directly once they’ve been cached, you need to properly configure your webserver to check for those static files.
- For nginx:Update your
location
block’stry_files
directive to include a check in thepage-cache
directory:location = / { try_files /page-cache/pc__index__pc.html /index.php?$query_string; } location / { try_files $uri $uri/ /page-cache/$uri.html /index.php?$query_string; }
- For apache:Open
public/.htaccess
and add the following before the block labeledHandle Front Controller
:# Serve Cached Page If Available... RewriteCond %{REQUEST_URI} ^/?$ RewriteCond %{DOCUMENT_ROOT}/page-cache/pc__index__pc.html -f RewriteRule .? page-cache/pc__index__pc.html [L] RewriteCond %{DOCUMENT_ROOT}/page-cache%{REQUEST_URI}.html -f RewriteRule . page-cache%{REQUEST_URI}.html [L]
Ignoring the cached files
To make sure you don’t commit your locally cached files to your git repository, add this line to your .gitignore
file:
/public/page-cache
Usage
Using the PublicBaseController
Since public controllers in Laraship are extending the Corals/core/Foundation/Http/Controllers/PublicBaseController.php
add page-cache middleware to list of middlewares
$this->corals_middleware = ['page-cache'];
Using the middleware
if you want to apply page cache to a specific route, you need to add the page-cache middleware to that route definition at web.php
To cache the response of a given request, use the page-cache
middleware:
Route::middleware('page-cache')->get('posts/{slug}', 'PostController@show');
Every post will now be cached to a file under the public/page-cache
directory, closely matching the URL structure of the request. All subsequent requests for this post will be served directly from disk, never even hitting your app!
Clearing the cache
Since the responses are cached to disk as static files, any updates to those pages in your app will not be reflected on your site. To update pages on your site, you should clear the cache with the following command:
php artisan page-cache:clear
You may optionally pass a URL slug to the command, to only delete the cache for a specific page:
php artisan page-cache:clear {slug}
Also if you like to be able to clear the cache from Cache management console in admin, edit the file Corals/core/Settings/config/settings.php
and the following code to the end of the array
'page-cache:clear' => [ 'text' => 'Clear Full Page Cache', 'class' => 'btn-warning' ]