1

I'm learning the Symfony framework and I'm having problems getting it to return the correct URL to a resource. I created a new bundle, Company/TestBundle, with this in my src/Company/TestBundle/Controller/DefaultController.php file:

<?php

namespace Company\TestBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{
    public function indexAction()
    {
        return $this->render('TestBundle:Default:index.html.twig');
    }
}

In src/Company/TestBundle/Resources/views/Default/index.html.twig:

{% extends "SmallworldBundle:Default:main.html.twig" %}

{%block title %}It's a small world after all!{% endblock %}

{% block body %} It's a small world! {% endblock %}

And in src/Company/TestBundle/Resources/views/Default/main.html.twig:

<html>
<head>
{% block stylesheets %}
<link rel = "stylesheet" href = "{{ asset('css/small.css') }}" type = "text/css" />
{% endblock %}
<title>{% block title %} {% endblock %}</title>
</head>
<body>
{%block body %}{% endblock %}
</body>
</html>

Now, I've run php app/console assets:install --symlink but even so, the generated code is trying to link to '/css/small.css' instead of where the file actually is, which is src/Company/TestBundle/Resources/public/css/small.css

Can someone explain why this isn't linking to the correct location?

2 Answers 2

1

Though NoScope's answer is a valid solution you are better off not introducing a hard dependency to web/bundles/testbundle.

You should use ...

{% block stylesheets '@CompanyTestBundle/Resources/public/css/small.css' %}
    <link rel = "stylesheet" href="{{ asset_url }}" type = "text/css" />
{% endblock %}

... instead.

This way if you change the way assetic installs your assets you will still have the correct path displayed.

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

Comments

0

You need to add the bundle name as well like:

{{ asset('bundles/testbundle/css/small.css') }}

Comments

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.