Symfony2 - 调用了错误的控制器操作

时间:2015-03-15 15:47:40

标签: symfony

我在页面上做了很多事情,我想我在某个地方遇到了冲突。基本上,我的页面最初显示一个输入和一个空白div。提供并提交输入后,页面将刷新div满数据。然后,用户可以选择其中一些数据,最后再次提交。

这是我的观点

{% block main %}
    <div class="col-md-4">
        <section class="panel panel-default">
            <header class="panel-heading">
                <h3 class="panel-title">Terminal</h3>
            </header>

            <div class="panel-body">
                <form action="{{ path('NickAlertBundle_terminalSearch') }}" method="post" enctype="multipart/form-data" class="terminalForm" id="terminalForm">
                    <div class="row">
                        <div class="col-md-12">
                            <input type="text" class="addMargin" id="terminal_command" name="terminal_command" placeholder=">">
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-8 col-md-offset-4">
                            <input type="submit" class="btn btn-default" id="terminal_submit" value="Submit">
                        </div>
                    </div>
                </form>
            </div>
        </section>
    </div>

    <div class="col-md-8" id="terminal-window">
        <table class="terminalAvailability">
            {% if data is defined %}
                <form action="{{ path('NickAlertBundle_terminalCreate') }}" method="post" enctype="multipart/form-data" class="terminalForm">
                    {% for info in data %}
                        <tr>
                            <td class="flightNumber">{{ info.flightNumber }}</td>
                            <td class="details">{{ info.from ~ info.to }}</td>
                            {% for seat, availability in info.seats %}
                                <td class="seatClass">
                                    <label for="{{ seat }}">
                                        <span>{{ seat ~ availability }}</span>
                                    </label>
                                    <input type="checkbox" id="{{ seat }}" name="seats[{{ info.flightNumber }}][]" style="display: none;" value="{{ seat }}" />
                                </td>
                            {% endfor %}
                            <td class="otherInfo">{{ info.other }}</td>
                        </tr>
                    {% endfor %}
                    <div class="row">
                        <div class="col-md-8 col-md-offset-4">
                            <input type="submit" class="btn btn-default" value="Submit">
                        </div>
                    </div>
                </form>
            {% endif %}
        </table>
    </div>

    <div class="modal"></div>
{% endblock %}

第一个div是输入,第二个div是div,数据将被显示,选择和重新提交。

然后我有我的控制器动作

public function terminalAction()
{
    return $this->render('NickAlertBundle:Page:terminal.html.twig');
}

public function terminalSearchAction(Request $request)
{
    try {
        $terminal_command = strtoupper($request->get('terminal_command'));

        $error = array();

        if (!$terminal_command) {
            $error[] = "Please enter the Command";
        }

        if (count($error)) {
            echo "There were errors adding the alert.\n\n";
            foreach ($error as $item) {
                echo $item . "\n";
            }
            die();
        }

        $uapiService = $this->container->get('alert_bundle.api_service');
        $commandData = $apiService->terminalService($terminal_command);

        return $this->render('NickAlertBundle:Page:terminal.html.twig', array(
            'data' => $commandData,
        ));

    }catch (Exception $e) {
    }
}

public function terminalCreateAction(Request $request)
{
    try {
        foreach ($request->request->get('seats') as $row) {
            foreach ($row as $seat) {
                var_dump($seat);
            }
        }
        return $this->render('NickAlertBundle:Page:terminal.html.twig');

    }catch (Exception $e) {
    }
}

最后我的路线

NickAlertBundle_terminal:
    pattern:  /terminal
    defaults: { _controller: NickAlertBundle:Alert:terminal }
    methods:  [GET]

NickAlertBundle_terminalSearch:
    pattern:  /terminal
    defaults: { _controller: NickAlertBundle:Alert:terminalSearch }
    methods:  [POST]

NickAlertBundle_terminalCreate:
    pattern:  /terminal
    defaults: { _controller: NickAlertBundle:Alert:terminalCreate }
    methods:  [POST]

因此页面最初显示正常。然后用户输入一些输入,提交它,然后响应数据显示在div中。所以这意味着前两条路线完美运行。使用div中的数据,用户可以选择一些数据,然后提交。但是,提交此数据时,它们将显示为

  

添加警报时出错。请输入命令

此错误适用于第二个操作,不应与第三个操作有任何关系。第二种形式的路径设置为NickAlertBundle_terminalCreate,为什么它会与其他动作交叉?

由于

1 个答案:

答案 0 :(得分:3)

NickAlertBundle_terminalCreate将始终解析为NickAlertBundle_terminalSearch,因为RouteMatcher将始终将模式/terminal +方法POST与具有这些规则的第一条路线匹配。

为什么不给NickAlertBundle_terminalSearch一个类似/terminal/search的模式?