如何在 Symfony2 路由/控制器中使用 YUI 压缩器

发布于 2025-01-07 16:26:59 字数 572 浏览 6 评论 0原文

如何在以下场景中使用 YUI 压缩器:

routing.yml

js_route:
   pattern:  /foo/bar.{_format}
   defaults: { _controller: FooBundle:Default:JS }
   requirements:
       _format: js

DefaultController.php

public function JSAction() {
   // ...
   // content for JS file is being generated
   // ...
   return $this->render('FooBundle:Default:bar.js.twig', $returnarray);
   // ...
}

我知道如何在我的树枝模板中使用它(例如 {% javascripts '@FooBundle/Resources/public/js/*' filter='?yui_js' %})但不幸的是不适用于上述情况。

有什么提示吗?谢谢!

How can I use the YUI compressor with following scenario:

routing.yml

js_route:
   pattern:  /foo/bar.{_format}
   defaults: { _controller: FooBundle:Default:JS }
   requirements:
       _format: js

DefaultController.php

public function JSAction() {
   // ...
   // content for JS file is being generated
   // ...
   return $this->render('FooBundle:Default:bar.js.twig', $returnarray);
   // ...
}

I know how to use it in my twig templates (e.g. {% javascripts '@FooBundle/Resources/public/js/*' filter='?yui_js' %}) but unfortunately not for above.

Any hints? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟─花易冷 2025-01-14 16:26:59

我实际上并不建议您这样做,因为 YUI JS 压缩器将在每次对资源的请求时加载。但这无论如何都是一种方法。

请注意,为了使示例保持简单,我排除了任何额外的代码来正确确定您的 Web 根目录和 jar 文件的位置。

$path = $this->container->getParameter('kernel.root_dir');
$ac = new \Assetic\Asset\AssetCollection(array(
    new \Assetic\Asset\FileAsset($path . '/../src/WebBundle/Resources/public/js/jquery.longclick.js')
), array(
    new \Assetic\Filter\Yui\JsCompressorFilter($path . '/Resources/java/yuicompressor-2.4.7.jar')
));
$compressJS = $ac->dump();
return new Response($compressJS, 200, array('Content-Type' => 'text/javascript'));

另请注意,您不仅限于 FileAsset()。还有其他可用的类,例如 StringAsset() 等,因此您可以动态构建内容。

I don't actually suggest you do this because the YUI JS compressor will be loaded on every request to the resource. But this is one way to do it anyway.

Note, in order to keep the example simple I've excluded any extra code to properly determine your web root and location of the jar file.

$path = $this->container->getParameter('kernel.root_dir');
$ac = new \Assetic\Asset\AssetCollection(array(
    new \Assetic\Asset\FileAsset($path . '/../src/WebBundle/Resources/public/js/jquery.longclick.js')
), array(
    new \Assetic\Filter\Yui\JsCompressorFilter($path . '/Resources/java/yuicompressor-2.4.7.jar')
));
$compressJS = $ac->dump();
return new Response($compressJS, 200, array('Content-Type' => 'text/javascript'));

Also note, you're not just limited to FileAsset(). There are other classes available like StringAsset(), etc, so you can build content dynamically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文