@@ -881,58 +881,37 @@ matter), Symfony2 uses the standard ``render`` helper to configure ESI tags:
881
881
882
882
.. code-block :: jinja
883
883
884
- {% render url('latest_news', { 'max': 5 }), {'standalone': true} %}
884
+ {# you can use a controller reference #}
885
+ {{ render_esi(controller('...:news', { 'max': 5 })) }}
886
+
887
+ {# ... or a URL #}
888
+ {{ render_esi(url('latest_news', { 'max': 5 })) }}
885
889
886
890
.. code-block :: php
887
891
892
+ <?php echo $view['actions']->render(
893
+ new ControllerReference('...:news', array('max' => 5)),
894
+ array('strategy' => 'esi'))
895
+ ?>
896
+
888
897
<?php echo $view['actions']->render(
889
898
$view['router']->generate('latest_news', array('max' => 5), true),
890
- array('standalone ' => true )
899
+ array('strategy ' => 'esi' )
891
900
) ?>
892
901
893
- .. include :: /book/_security-2012-6431.rst.inc
894
-
895
- The ``render `` tag takes the absolute url to the embedded action. This means
896
- that you need to define a new route to the controller that you're embedding:
897
-
898
- .. code-block :: yaml
899
-
900
- # app/config/routing.yml
901
- latest_news :
902
- pattern : /esi/latest-news/{max}
903
- defaults : { _controller: AcmeNewsBundle:News:news }
904
- requirements : { max: \d+ }
905
-
906
- .. caution ::
907
-
908
- Unless you want this URL to be accessible to the outside world, you
909
- should use Symfony's firewall to secure it (by allowing access to your
910
- reverse proxy's IP range). See the :ref: `Securing by IP<book-security-securing-ip> `
911
- section of the :doc: `Security Chapter </book/security >` for more information
912
- on how to do this.
913
-
914
- .. tip ::
902
+ By using the ``esi `` rendering strategy (via the ``render_esi `` Twig
903
+ function), you tell Symfony2 that the action should be rendered as an ESI tag.
904
+ You might be wondering why you would want to use a helper instead of just
905
+ writing the ESI tag yourself. That's because using a helper makes your
906
+ application work even if there is no gateway cache installed.
915
907
916
- The best practice is to mount all your ESI urls on a single prefix (e.g.
917
- ``/esi ``) of your choice. This has two main advantages. First, it eases
918
- the management of ESI urls as you can easily identify the routes used for ESI.
919
- Second, it eases security management since securing all urls starting
920
- with the same prefix is easier than securing each individual url. See
921
- the above note for more details on securing ESI URLs.
922
-
923
- By setting ``standalone `` to ``true `` in the ``render `` Twig tag, you tell
924
- Symfony2 that the ac
9E88
tion should be rendered as an ESI tag. You might be
925
- wondering why you would want to use a helper instead of just writing the ESI tag
926
- yourself. That's because using a helper makes your application work even if
927
- there is no gateway cache installed.
928
-
929
- When standalone is ``false `` (the default), Symfony2 merges the included page
930
- content within the main one before sending the response to the client. But
931
- when standalone is ``true ``, *and * if Symfony2 detects that it's talking
932
- to a gateway cache that supports ESI, it generates an ESI include tag. But
933
- if there is no gateway cache or if it does not support ESI, Symfony2 will
934
- just merge the included page content within the main one as it would have
935
- done were standalone set to ``false ``.
908
+ When using the default ``render `` function (or setting the strategy to
909
+ ``default ``), Symfony2 merges the included page content within the main one
910
+ before sending the response to the client. But when using ``esi `` strategy,
911
+ *and * if Symfony2 detects that it's talking to a gateway cache that supports
912
+ ESI, it generates an ESI include tag. But if there is no gateway cache or if
913
+ it does not support ESI, Symfony2 will just merge the included page content
914
+ within the main one as it would have done if you had used ``render ``.
936
915
937
916
.. note ::
938
917
@@ -947,14 +926,52 @@ of the master page.
947
926
948
927
public function newsAction($max)
949
928
{
950
- // ...
929
+ // ...
951
930
952
- $response->setSharedMaxAge(60);
931
+ $response->setSharedMaxAge(60);
953
932
}
954
933
955
934
With ESI, the full page cache will be valid for 600 seconds, but the news
956
935
component cache will only last for 60 seconds.
957
936
937
+ When using a controller reference, the ESI tag should reference the embedded
938
+ action as an accessible URL so the gateway cache can fetch it independently of
939
+ the rest of the page. Of course, an action can't be accessed via a URL unless
940
+ it has a route that points to it. Symfony2 takes care of this via a generic
941
+ route and controller. For the ESI include tag to work properly, you must
942
+ define the ``_proxy `` route:
943
+
944
+ .. configuration-block ::
945
+
946
+ .. code-block :: yaml
947
+
948
+ # app/config/routing.yml
949
+ _proxy :
950
+ resource : " @FrameworkBundle/Resources/config/routing/proxy.xml"
951
+ prefix : /proxy
952
+
953
+ .. code-block :: xml
954
+
955
+ <!-- app/config/routing.xml -->
956
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
957
+
958
+ <routes xmlns =" http://symfony.com/schema/routing"
959
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
960
+ xsi : schemaLocation =" http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd" >
961
+
962
+ <import resource =" @FrameworkBundle/Resources/config/routing/proxy.xml" prefix =" /proxy" />
963
+ </routes >
964
+
965
+ .. code-block :: php
966
+
967
+ // app/config/routing.php
968
+ use Symfony\Component\Routing\RouteCollection;
969
+ use Symfony\Component\Routing\Route;
970
+
971
+ $collection->addCollection($loader->import('@FrameworkBundle/Resources/config/routing/proxy.xml', '/proxy'));
972
+
973
+ return $collection;
974
+
958
975
One great advantage of this caching strategy is that you can make your
959
976
application as dynamic as needed and at the same time, hit the application as
960
977
little as possible.
@@ -967,7 +984,7 @@ little as possible.
967
984
obey the ``max-age `` directive and cache the entire page. And you don't
968
985
want that.
969
986
970
- The ``render `` helper supports two other useful options:
987
+ The ``render_esi `` helper supports two other useful options:
971
988
972
989
* ``alt ``: used as the ``alt `` attribute on the ESI tag, which allows you
973
990
to specify an alternative URL to be used if the ``src `` cannot be found;
0 commit comments