From d51287520174de78c460b59f7fd4090a6e4b22bc Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 16 Nov 2017 16:10:57 +0100 Subject: [PATCH] Updated http_cache/* articles to Symfony 4 --- http_cache.rst | 26 ++++++++++++-------------- http_cache/cache_invalidation.rst | 2 +- http_cache/esi.rst | 16 ++++++++-------- http_cache/validation.rst | 6 +++--- 4 files changed, 24 insertions(+), 26 deletions(-) diff --git a/http_cache.rst b/http_cache.rst index 21375c4dcee..9b42e54a966 100644 --- a/http_cache.rst +++ b/http_cache.rst @@ -81,18 +81,15 @@ Enabling the proxy is easy: each application comes with a caching kernel (``AppC that wraps the default one (``AppKernel``). The caching Kernel *is* the reverse proxy. -To enable caching, modify the code of your front controller. You can also make these -changes to ``index.php`` to add caching to the ``dev`` environment:: +To enable caching, modify the code of your ``index.php`` front controller:: // public/index.php use Symfony\Component\HttpFoundation\Request; // ... - $kernel = new AppKernel('prod', false); - $kernel->loadClassCache(); + $kernel = new Kernel($_SERVER['APP_ENV'] ?? 'dev', $_SERVER['APP_DEBUG'] ?? false); - // add (or uncomment) this new line! - // wrap the default Kernel with the AppCache one + // add (or uncomment) this line to wrap the default Kernel with the AppCache one $kernel = new AppCache($kernel); $request = Request::createFromGlobals(); @@ -120,7 +117,9 @@ finely tuned via a set of options you can set by overriding the :method:`Symfony\\Bundle\\FrameworkBundle\\HttpCache\\HttpCache::getOptions` method:: - // app/AppCache.php + // src/AppCache.php + namespace App; + use Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache; class AppCache extends HttpCache @@ -137,10 +136,9 @@ method:: For a full list of the options and their meaning, see the :method:`HttpCache::__construct() documentation `. -When you're in debug mode (either because your booting a ``debug`` kernel, like -in ``index.php`` *or* you manually set the ``debug`` option to true), Symfony -automatically adds an ``X-Symfony-Cache`` header to the response. Use this to get -information about cache hits and misses. +When you're in debug mode (the second argument of ``Kernel`` constructor in the +front controller is ``true``), Symfony automatically adds an ``X-Symfony-Cache`` +header to the response. Use this to get information about cache hits and misses. .. _http-cache-symfony-versus-varnish: @@ -150,7 +148,7 @@ information about cache hits and misses. website or when you deploy your website to a shared host where you cannot install anything beyond PHP code. But being written in PHP, it cannot be as fast as a proxy written in C. - + Fortunately, since all reverse proxies are effectively the same, you should be able to switch to something more robust - like Varnish - without any problems. See :doc:`How to use Varnish ` @@ -192,7 +190,7 @@ These four headers are used to help cache your responses via *two* different mod All of the HTTP headers you'll read about are *not* invented by Symfony! They're part of an HTTP specification that's used by sites all over the web. To dig deeper - into HTTP Caching, check out the documents `RFC 7234 - Caching`_ and + into HTTP Caching, check out the documents `RFC 7234 - Caching`_ and `RFC 7232 - Conditional Requests`_. As a web developer, you are strongly urged to read the specification. Its @@ -214,7 +212,7 @@ The *easiest* way to cache a response is by caching it for a specific amount of use Symfony\Component\HttpFoundation\Response; // ... - public function indexAction() + public function index() { // somehow create a Response object, like by rendering a template $response = $this->render('blog/index.html.twig', []); diff --git a/http_cache/cache_invalidation.rst b/http_cache/cache_invalidation.rst index a67adb470c4..1cd44c00813 100644 --- a/http_cache/cache_invalidation.rst +++ b/http_cache/cache_invalidation.rst @@ -50,7 +50,7 @@ cache instead of going to the application to get a response. Here is how you can configure the Symfony reverse proxy to support the ``PURGE`` HTTP method:: - // app/AppCache.php + // src/AppCache.php use Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache; use Symfony\Component\HttpFoundation\Request; diff --git a/http_cache/esi.rst b/http_cache/esi.rst index 6276bf5110e..e5a7d46eac9 100644 --- a/http_cache/esi.rst +++ b/http_cache/esi.rst @@ -62,14 +62,14 @@ First, to use ESI, be sure to enable it in your application configuration: .. code-block:: yaml - # app/config/config.yml + # config/packages/framework.yaml framework: # ... esi: { enabled: true } .. code-block:: xml - + loadFromExtension('framework', array( // ... 'esi' => array('enabled' => true), @@ -104,7 +104,7 @@ independent of the rest of the page. // ... class DefaultController extends Controller { - public function aboutAction() + public function about() { $response = $this->render('static/about.html.twig'); // set the shared max age - which also marks the response as public @@ -195,7 +195,7 @@ of the master page. // ... class NewsController extends Controller { - public function latestAction($maxPerPage) + public function latest($maxPerPage) { // ... $response->setSharedMaxAge(60); @@ -220,14 +220,14 @@ that must be enabled in your configuration: .. code-block:: yaml - # app/config/config.yml + # config/packages/framework.yaml framework: # ... fragments: { path: /_fragment } .. code-block:: xml - + loadFromExtension('framework', array( // ... 'fragments' => array('path' => '/_fragment'), diff --git a/http_cache/validation.rst b/http_cache/validation.rst index 97dff6f41a0..260c7667eb5 100644 --- a/http_cache/validation.rst +++ b/http_cache/validation.rst @@ -66,7 +66,7 @@ To see a simple implementation, generate the ETag as the md5 of the content:: class DefaultController extends Controller { - public function homepageAction(Request $request) + public function homepage(Request $request) { $response = $this->render('static/homepage.html.twig'); $response->setEtag(md5($response->getContent())); @@ -131,7 +131,7 @@ header value:: class ArticleController extends Controller { - public function showAction(Article $article, Request $request) + public function show(Article $article, Request $request) { $author = $article->getAuthor(); @@ -190,7 +190,7 @@ exposing a simple and efficient pattern:: class ArticleController extends Controller { - public function showAction($articleSlug, Request $request) + public function show($articleSlug, Request $request) { // Get the minimum information to compute // the ETag or the Last-Modified value