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.

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

报告相同问题?

悬赏问题

  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致