doudao7113 2016-02-19 10:59
浏览 56
已采纳

Symfony和数据输入

Currently I am working on a controller class which should be able to let a customer add an item to a table in a database and view these items (e.g. a movie).

I am using the following twig code:

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

{% block body %}
    {% if movies|length == 0 %}
        There are no movie items available. Add a movie <a href="#">here</a> to get started.
    {% elseif movies|length != 0 %}
        These are the results: ...
    {% endif %}
{% endblock %}

What is the best way to let a user/customer or whatever the case, add an item to the table that needs to be shown? Should I let a user fill a form on the exact same page as the overview template (I personally do not think this is good, as I want to keep purpose of each page seperated) or should I make a different template with a form where the user will be send to (though this takes redirection time, which some users might be getting annoyed by)? As you can see I am using an anchor tag on the word "here". How should I set-up the anchor tag if I am to use a different template for creating records in a table?

As the documentation of Symfony shows the following:

<a href="{{ path('_welcome') }}">Home</a>

The path says _welcome and I think it refers to the name of the route that points to a certain controller. Would it be best to use this path function and what would I need to add where it now says _welcome (or was I correct one sentence ago)? And why is there an underscore in the given example?

I am asking this question because when I worked with ASP.NET MVC, there was this method called ActionLink() and made me wonder if this is most common use of redirecting, since you could also just add the template file location to the anchor tag href attribute?

  • 写回答

1条回答 默认 最新

  • donglang6656 2016-02-19 11:49
    关注

    If the form to add a new item is small (one text field + one submit button), you could add the form in the same page.

    For example :

    {% extends 'base.html.twig' %}
    
    {% block body %}
    
    // display your form here
    
        {% if movies|length == 0 %}
            There are no movie items available.
        {% elseif movies|length != 0 %}
            These are the results: ...
        {% endif %}
    {% endblock %}
    

    But actually it's up to you to decide if you want it to be displayed in the same page or not.

    In the case you decide to redirect the user to a new template, especially for the form, you write the name of the route of the correspondant controller :

    <a href="{{ path('your_route_name') }}">here</a>
    

    So the code of the controller will be executed and will redirect to the page of your form.

    Concerning "_welcome", I don't know why they write it like this. This is more the way to name a layout file than a route name.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式