Barrosy
Barrosy

Reputation: 1457

Symfony and data input

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?

Upvotes: 0

Views: 75

Answers (1)

Melody
Melody

Reputation: 74

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.

Upvotes: 1

Related Questions