【问题标题】:Symfony 2 Assetic css and js 404 within production environment生产环境中的 Symfony 2 Assetic css 和 js 404
【发布时间】:2015-03-06 11:29:50
【问题描述】:

我已经安装了 Symphony2 框架并创建了自己的包。 我正在为我的 js 和 css 文件使用资产。

我在我的服务器上运行 ubuntu,在我的本地机器上运行 mint。

当我在本地访问 app_dev.php 时,所有资产都可以正常运行。

当我在本地访问 app.php 时,所有资产都可以正常运行。

但是在我的服务器上,路由被渲染,但资产(css 和 js)我得到 404。

当我跟踪 prod.log 时,我在下面得到一个未捕获的异常:

 PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "No route found for "GET /admin/css/875a243.css""

我在网上到处搜索,但似乎无法弄清楚。

我已经清除缓存,资产转储,资产安装,所有权限都正确。

我的应用程序 routing.yml 配置:

    brs:
  resource: "@BrsAdminBundle/Resources/config/routing.yml"
  prefix: /

我的 bundle routing.yml 配置

admin:
  path: /admin/
  defaults: { _controller: BrsAdminBundle:Admin:index }

我的应用配置:

imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: assets.yml }

framework:
    #esi:             ~
    #translator:      { fallback: "%locale%" }
    secret:          "%secret%"
    router:
        resource: "%kernel.root_dir%/config/routing.yml"
        strict_requirements: ~
    form:            ~
    csrf_protection: ~
    validation:      { enable_annotations: true }
    templating:
        engines: ['twig']
        #assets_version: SomeVersionScheme
    default_locale:  "%locale%"
    trusted_proxies: ~
    session:         ~
    fragments:       ~
    http_method_override: true

# Twig Configuration
twig:
    debug:            "%kernel.debug%"
    strict_variables: "%kernel.debug%"

# Assetic Configuration
assetic:
    debug:          "%kernel.debug%"
    use_controller: false
    bundles:        [ ]
    #java: /usr/bin/java
    filters:
        cssrewrite: ~
        #closure:
        #    jar: "%kernel.root_dir%/Resources/java/compiler.jar"
        #yui_css:
        #    jar: "%kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar"

# Doctrine Configuration
doctrine:
    dbal:
        driver:   "%database_driver%"
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8
        # if using pdo_sqlite as your database driver:
        #   1. add the path in parameters.yml
        #     e.g. database_path: "%kernel.root_dir%/data/data.db3"
        #   2. Uncomment database_path in parameters.yml.dist
        #   3. Uncomment next line:
        #     path:     "%database_path%"

    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        auto_mapping: true

# Swiftmailer Configuration
swiftmailer:
    transport: "%mailer_transport%"
    host:      "%mailer_host%"
    username:  "%mailer_user%"
    password:  "%mailer_password%"
    spool:     { type: memory }

我的asset.yml 配置:

assetic:
  assets:
    bootstrap_js:
      inputs:
        - '%Kernel.root_dir%/Resources/public/js/jquery-2.1.3.min.js'
        - '%Kernel.root_dir%/Resources/public/js/bootstrap.min.js'
    bootstrap_css:
      inputs:
        - '%Kernel.root_dir%/Resources/public/css/bootstrap.min.css'
        - '%Kernel.root_dir%/Resources/public/css/bootstrap-theme.min.css'
    admin_css:
      inputs:
        - '@BrsAdminBundle/Resources/public/css/styles.css'

我使用assetic的base.html.twig:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>{% block title %}BankRoll Supply{% endblock %}</title>
        {% block stylesheets %}
            {% stylesheets '@bootstrap_css' '@admin_css' %}
                <link rel="stylesheet" type="text/css" href="{{ asset_url }}" />
            {% endstylesheets %}
        {% endblock %}
        <link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}" />
    </head>
    <body>
        {% block body %}{% endblock %}
        {% block javascripts %}
            {% javascripts '@bootstrap_js' %} 
                <script type="text/javascript" src="{{ asset_url }}"></script>
            {% endjavascripts %}
        {% endblock %}
    </body>
</html>

任何帮助将不胜感激。

谢谢,

广告

【问题讨论】:

    标签: php symfony routing http-status-code-404 assetic


    【解决方案1】:

    您好,我认为您遇到了 cssrewrite 问题。

    你有两个选择;

    这是第一个选项的示例

    您可以为资产指定输出目录。下面的示例指向 '/web/bundles/yourbundle/css' 执行 assets:install --env=prodassetic:dump --env=prod,我认为你很高兴。

    {% stylesheets
    '@admin_css'
    output='bundles/yourbundle/css/admin_style.css'
    %}
    <link href="{{ asset_url }}?{{ AssetVersion }}" rel="stylesheet" type="text/css">
    {% endstylesheets %}
    

    另外我不得不提一下,当你在asset.yml 中定义你的资产时,assetic:dump 会在你的公共资产目录中生成一个assets 文件夹,在这种情况下你不需要它。要摆脱该文件夹,您可以在 twig 中使用此语法;

     {% stylesheets
    '@YourBundle/Resources/public/css/admin.css'
    output='bundles/yourbundle/css/admin_style.css'
    %}
    <link href="{{ asset_url }}?{{ AssetVersion }}" rel="stylesheet" type="text/css">
    {% endstylesheets %}
    

    【讨论】:

    • 添加 --end=prod 对我来说已经足够了。 (assets:install --end=prod) 没有它,它需要大约 5 分钟并且之后不起作用(没有错误)。加上这个,它花了一两秒钟就可以工作了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-23
    • 1970-01-01
    • 1970-01-01
    • 2019-07-24
    • 1970-01-01
    • 2021-07-04
    相关资源
    最近更新 更多