2

In Symfony2's routing.yml, I understand it's possible to route to a static HTML page, like this:

my_static_route:
    path:     /mycomponent
    defaults:
        _controller: FrameworkBundle:Template:template
        template:    @StubbornShowaBundle/Resources/public/imports/component.html

My question is, is there any way to do variable routing in routing.yml using FrameworkBundle:Template:template?

Like this(doesn't work, just an image of what I want to do):

my_static_route:
    path:     /mycomponents/{file}
    defaults:
        _controller: FrameworkBundle:Template:template
        template:    @StubbornShowaBundle/Resources/public/imports/{file}

And then /mycomponents/foobar.html would load @StubbornShowaBundle/Resources/public/imports/foobar.html

Is that possible? (And if so, how do I do that?)

3 Answers 3

1

You need to create custom controller:

namespace Stubborn\ShowaBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class StaticController extends Controller
{
    public function streamAction($filename)
    {
        try {
            $path = $this->container->get('kernel')->locateResource('@StubbornShowaBundle/Resources/public/imports/' . $filename);
            $response = new BinaryFileResponse($path);

            return $response;
        } catch (\InvalidArgumentException $e) {
            throw $this->createNotFoundException(sprintf("File '%s' not exists!", $filename));
        }
    }
}

And define route like:

my_static_route:
    path:     /mycomponents/{filename}
    defaults:
        _controller: StubbornShowaBundle:Static:stream
Sign up to request clarification or add additional context in comments.

3 Comments

This seems to render the template as a twig resource. The reason I suspect so is because Symfony throws exception errors when you load Polymer files, which uses the same mustache syntax as Twig.
I do not want to use BinaryFileResponse since it's for serving files(see the Symfony API docs), but I replaced $response = new BinaryFileResponse($path); with $response = new Response(file_get_contents($path)); and that does pretty much what I want, so I'll give you an upvote for giving me 90% of the answer. (If you fix the part about BinaryFileResponse I will consider it 100% of the answer and accept it)
I won't change my answer, because – technically – you are serving static files.
0

Let the template decided by controller. You can pass variable file to Controller.

// In your TemplateController.
public function templateAction($file)
{
    return $this->render('@StubbornShowaBundle/Resources/public/imports/' . $file);
}

This way, the dynamic template file name received from the url will be rendered.

Hope this helps!

5 Comments

The problem is, I do not want to render a dynamic template file, but serve static HTML.
You can put the static HTML in several files. Just pass the file name in the controller to render that particular HTML file. Here file name is dynamic for controller not HTML.
No offense, but you seem to be misunderstanding the problem. You say "pass the file name in the controller to render that particular HTML file", but rendering it is exactly what I want to avoid. You are using Controller's function render, and to quote the Symfony API manual, that function "Renders a view."
Any specific reason, why you want to avoid rendering an HTML view?
One specific reason would be that there are cases where certain HTML files are not written with the premise that they will be processed with twig, meaning they might contain unintentional use of twig syntax, which causes errors(To give a concrete example, Polymer). A more general reason would be the fact that it is completely pointless to render something which itself is the result of a template-rendering, and that it makes no sense to me whatsoever to render something because I have no reason not to, if anything it seems like a line of reasoning that invites an increase in technical debt.
0

You could either have a template that managed everything or use it to delegate to other secondary templates.

{% extends 'WhatEver.html.twig' %}

{% block something %}
    {# handle in template #}
    {% if 'value' == file %}
        <ul>
            <li>{{file}}</li>
            <li>Whatever</li>
            <li>You</li>
            <li>Would</li>
            <li>Have</li>
            <li>In</li>
            <li>The</li>
            <li>Template</li>
        </ul>
    {% else %}
        <div class="alert">
            File "{{file}}" not recognised!
        </div>
    {% endif %}

    {# delegate to separate template #}
    {{ include('@StubbornShowaBundle/Resources/public/imports/' ~ file, ignore_missing = true) }}
    {# or maybe (to add the extension) #}
    {{ include('@StubbornShowaBundle/Resources/public/imports/' ~ file ~ '.html.twig', ignore_missing = true) }}
{% endblock %}

1 Comment

The question is specifically about routing to static HTML, as opposed to using templates.

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.