【问题标题】:Symfony2 - Bundle with AngularJSSymfony2 - 与 AngularJS 捆绑
【发布时间】:2014-05-01 11:47:32
【问题描述】:

我正在做一个主要基于 Symfony2 和 Twig-Templates 的项目。我现在确实想编写一个基于 AngularJS 的新包。也就是说,大部分站点的菜单和导航仍然在 php/Twig 中,但用户列表应该用 AngularJS 编写。 我已经启动并运行了 AngularJS 前端,其文件结构看起来像这样:

  • 应用/
    • js/
      • app.js
    • 部分/
      • users.html
      • user_detail.html
    • index.html
  • bower_components/
  • ...

现在,我只想在 Symfony2 中的给定路由下显示它。 有没有办法说我不需要 Symfony 中的任何前端控制器 - 只是静态交付?

对于此类应用程序,一般合理的方法是什么? 我发现的大多数教程都假设整个网站都是用 Angular 编写的,而不仅仅是一个包。

感谢您的任何提示!

【问题讨论】:

  • 将你的部分和 js 移动到 Symfony 的 web 目录中,它们将被静态地提供。我尝试了各种方法。我通常会使用特定于捆绑包的 PartialsControllers 来提供位于多个捆绑包中的部分。

标签: javascript php angularjs symfony


【解决方案1】:

我之前使用过asoc/assetic-angular-js-bundle (GitHub),当它与资产过滤器配置一起使用时,意味着所有部分和脚本都被合并到一个文件中。

他们的 github 页面说要在你的 twig 模板中使用以下内容..

{% javascripts filter="angular"
    '@BundleName/Resources/views/aTemplate.html.ng'
    '@BundleName/Resources/views/fooTemplate.html.ng'
    '@BundleName/Resources/views/moarTemplates/*.html.ng'
    %}
        <script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}

但是如果你使用..

// app/config/config.yml
assetic:
    filters:
        angular:
            apply_to: "\.ng$"
            resource: %kernel.root_dir%/../vendor/asoc/assetic-angular-js-bundle
                      /Asoc/AsseticAngularJsBundle/Resources/config/assetic.xml
                      // This is all one line
                      // Not sure why this was needed, I vaguely remember
                      // assetic not being able to find to config

然后你可以像这样在你的 twig javascripts 部分调用你的部分..

{% javascripts
    '@BundleName/Resources/views/angular-app.js'
    '@BundleName/Resources/views/angular-controllers.js'
    '@BundleName/Resources/views/some-other-stuff.js'
    '@BundleName/Resources/views/aTemplate.html.ng'
    '@BundleName/Resources/views/fooTemplate.html.ng'
    '@BundleName/Resources/views/moarTemplates/*.html.ng'
    %}
        <script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}

然后我想您可以将上述内容包含在您希望它显示的一个树枝视图中,也可以将其包含在您的所有模板中(取决于您希望如何处理脚本缓存以及不包含的内容)和只需使用常规的 ng-view UserListController 类型标签调用它即可。

对不起,如果这与您的要求无关。在我开始写这篇文章的时候,它很有道理,但是我怎么看它这么多,看起来我刚刚走开了一个完整的切线。

【讨论】: