eglobal / template-cache-bundle
Symfony 模板缓存功能。
1.0.8
2020-03-23 14:33 UTC
Requires
- php: >=7.1
- doctrine/annotations: ~1.0
- psr/log: ~1.0
- sensio/framework-extra-bundle: ^5.2
- symfony/asset: ^4.0
- symfony/console: ^4.0
- symfony/framework-bundle: ^4.0
- symfony/routing: ^4.0
- symfony/templating: ^4.0
- symfony/translation: ^4.0
- symfony/twig-bundle: ^4.0
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-27 10:20:09 UTC
README
安装
使用Composer安装此包。在你的composer.json文件中添加以下内容
{ "require": { "eglobal/template-cache-bundle": "~1.0" } }
在app/AppKernel.php中注册包
public function registerBundles() { $bundles = [ // ... new EGlobal\Bundle\TemplateCacheBundle\EGlobalTemplateCacheBundle(), ]; }
更新配置
parameters: locales: - en - es - de # If using Assetic, add the bundle to the config assetic: bundles: - EGlobalTemplateCacheBundle eglobal_template_cache: # Locales to be cached locales: "%locales%" # Cache only exposed routes exposed_routes_only: false # Public directory to store cached templates cache_dir: '%kernel.root_dir%/../web/templates' # Public prefix of cached templates public_prefix: '/templates' # Directories to search cacheable templates in root_dirs: - "@AcmeFooBundle/Controller" - "@AcmeBarBundle/Controller/Cacheable"
示例用法
标记控制器路由为可缓存
<?php namespace Acme\FooBundle\Controller; use EGlobal\Bundle\TemplateCacheBundle\Annotation\CacheableTemplate; use Symfony\Component\Routing\Annotation\Route; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class MyController extends Controller { /** * @Route(methods={"GET"}, path="/my/foo.html", name="my.template.foo", options={"expose"=true}) * @CacheableTemplate("AcmeFooBundle:Template:foo.html.twig") */ public function fooAction() { // Your controller logic } /** * @Route(methods={"GET"}, path="/my/bar.svg", name="my.template.bar", options={"expose"=true}) * @CacheableTemplate("AcmeFooBundle:Template:bar.svg.twig") */ public function barAction() { // Your controller logic } }
将模板转储到缓存文件
$ php bin/console eglobal:template-cache:dump
将资源添加到你的模板
... <head> <script type="text/javascript" src="{{ asset('bundles/eglobaltemplatecache/js/template-cache.js') }}"></script> {% if not app.debug %} <script type="text/javascript" src="{{ asset(jsTemplateMapFileName(app.request.locale)) }}"></script> {% endif %} </head> ...
在代码中使用可缓存模板路径
// This will return some path like '/templates/en/f9d15a8be554432de01799a8c51d123f.html' var fooCachedUrl = TemplateCache.get('my.template.foo'); // This will return some path like '/templates/en/f9d15a8be554432de01799a8c51d123f.svg' var barCachedUrl = TemplateCache.get('my.template.bar');