2

I've been searching for this for 4 days now and couldn't find a working solution. I want to make Symfony2 work on shared hosting without access to command line or httpd.conf (there's no way to set virtual host). All I can do, is just edit .htaccess files. In my web root directory I also have some other projects (like forum). The directory structure is:

public_html
 |-forum
 |-ox
 '-Symfony
    |-app
    |-bin
    <...>

I can make it work both in dev ant prod environments (routing works well), BUT it doesn't load any assets (js, css, images). In error log there's always the same:

request.ERROR: Symfony\Component\HttpKernel\Exception\NotFoundHttpException: No route found for "GET /bundles/acmedemo/images/welcome-demo.gif" (uncaught exception)

Same happens if asset is loaded not from bundles, but also in twig as:

{{ asset('css/main.css') }}

Then it ends up with

request.ERROR: Symfony\Component\HttpKernel\Exception\NotFoundHttpException: No route found for "GET /css/main.css" (uncaught exception)

My .htaccess in public_html is:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# DEV ENVIRONMENT #
RewriteRule ^$ Symfony/web/app_dev.php [QSA]
RewriteRule ^(.*)$ Symfony/web/app_dev.php/$1 [QSA,L]

# PROD ENVIRONMENT #
#RewriteRule ^$ Symfony/web/app.php [QSA]
#RewriteRule ^(.*)$ Symfony/web/app.php/$1 [QSA,L]

Any suggestions how to make things right?

2 Answers 2

2

Interesting problem. After digging around the code I found following solution.

Create a class named PathPackage.php in src/Vendor/YourBundle/Templating/Asset folder with following code.

<?php

namespace Vendor\YourBundle\Templating\Asset;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Templating\Asset\PathPackage as BasePathPackage;

class PathPackage extends BasePathPackage
{
    /**
     * Constructor.
     *
     * @param Request $request The current request
     * @param string  $version The version
     * @param string  $format  The version format
     */
    public function __construct(Request $request, $version = null, $format = null)
    {
        parent::__construct("/Symfony", $version, $format);
    }
}

Then in your app/config/config.yml add the following parameter.

parameters:
  // ...
  templating.asset.path_package.class: Vendor\YourBundle\Templating\Asset\PathPackage

Now it will append /Symfony to the asset url parameter.

To summarize asset twig function calls getUrl method to determine the url. Which is extended by this class. Object of the class is passed as argument during templating.helper.assets service creation. Luckily PathPackage class is configurable. So solution was possible :).

Sign up to request clarification or add additional context in comments.

3 Comments

If I just add a line to config.yml, then I get an error: InvalidArgumentException: There is no extension able to load the configuration for "templating.asset.path_package.class" (in ...app\config\config.yml). Looked for namespace "templating.asset.path_package.class", found "framework", "security", "twig", "monolog", "swiftmailer", "doctrine", "assetic", "sensio_framework_extra", "jms_security_extra", "acme_demo", "web_profiler", "sensio_distribution" Isn't such parameters confugured just in XML? And if so, I couldn't figure out how to do it :(
You have to add it in parameters section. Edited my comment.
Then I get ErrorException: Catchable Fatal Error: Argument 1 passed to Cronus\TplBundle\Templating\Asset\PathPackage::__construct() must be an instance of Symfony\Component\HttpFoundation\Request, none given, called in ...app\AppKernel.php on line 21 and defined in ...src\Cronus\TplBundle\Templating\Asset\PathPackage.php line 17 I think I'll just keep all CSS files and images in web/bundles... and use asset('bundles/...') cause as I see there's no simple way to do it and I have no time (after a week of searching)
1

Do php app/console assets:install ./web locally, and upload the content of web folder your remote shared hosting.

5 Comments

How can I do it in xampp running on Win7? Googled for it, but no result.
OK.. I've got it working at last, but when I execute a command, I get Could not open input file: app/console
@CRONUS Are you in Symfony folder?
OK. It installs assets from bundles, but it's still the same with assets which are used globally - like from \app\Resources\css\. These are not installed anywhere after executing the command. I also couldn't find any documentation about this - maybe it's not possible and these globally used css files or images should be in every bundle? If so, then it worked for me from the start :) But I think if it's correctly loaded without htaccess from app.php/css/... then it should be also available to use with rewrite.
I just found out that it works normally if i just use asset like <link rel="stylesheet" href="{{ asset('bundles/cronustitle/css/core.css') }}" type="text/css" media="all" /> BUT it doesn't work if I use {% stylesheets ... %}<link rel="stylesheet" href="{{ asset_url }}" />{% endstylesheets %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.