Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2 Assetic css and js 404 within production environment

I have installed Symphony2 framework and created my own bundle. I am using assetic for my js and css files.

I am running ubuntu on my server and mint on my local machine.

When I access the app_dev.php locally all assets serve just fine.

When I access the app.php locally all assets serve just fine.

However on my server, the route gets rendered but the assets (css & js) i get a 404.

When i tail the prod.log i get an uncaught Exception below:

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

I have searched the web high and low and i cannot seem to figure this out.

I have cleared caches, assetic dumped, assets install , all permissions are correct.

my app routing.yml config:

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

my bundle routing.yml config

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

my app config:

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 }

my asset.yml config:

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'

my base.html.twig that uses assetic:

<!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>

Any help would be greatly appreciated.

Thank you,

Ad

like image 303
Adamski Avatar asked Jan 08 '15 11:01

Adamski


1 Answers

Hi I think you are having issues because of cssrewrite.

You have two options;

  • Specify output directory

  • Point your files manually and use cssrewrite filter in twig. See here; http://symfony.com/doc/current/cookbook/assetic/asset_management.html#fixing-css-paths-with-the-cssrewrite-filter

This is an example of the first option

You can specify an output directory for your assets. The example below points to '/web/bundles/yourbundle/css' Do an assets:install --env=prod and assetic:dump --env=prod and I think you are good to go.

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

Also I have to mention that when you define your assets in asset.yml, assetic:dump generates an assets folder in your public assets directory, which you don't need in this case. To get rid of that folder you can use this syntax in 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 %}
like image 169
Ugur Avatar answered Nov 05 '22 20:11

Ugur