ZF3 redirect()->toUrl() 不重定向

ZF3 redirect()->toUrl() not redirecting

我在使用 ZF3 时遇到了一个奇怪的问题。 我在视图中有一个香草形式和一个 jquery ajax 将其发送到控制器,如下所示:

<form>some form</form>
<script>
    $("#form").submit(function (e) {
        e.preventDefault();
        $.ajax({
            method: "POST",
            url: "stats",
            data: {name: 'TEST'} // name selected in the form
        });
    });
</script>

操作统计控制器如下所示:

$stat = new Stat();

$route_name = $this->params()->fromRoute('name', 'none');
$post_name = $this->params()->fromPost('name', 'none');

if(!strcmp($route_name, 'none')) // if no redirection yet
{
    if(!strcmp($post_name, 'none')) // if no form was sent
    {
        // display the form to choose the customer
        return new ViewModel([
            'customer_list' => $stat->get_customer_list(),
        ]);
    }
    else // if the form was sent, get name and direct to /stats/someName
    {
        return $this->redirect()->toRoute('stats', ['name' => 'someName']);
    }
}
else // after redirection, get the name in the URL and show some data about this customer
{
    return new ViewModel([
        'avg_time' => $stat->get_avg_time(rawurldecode($route_name)),
    ]);
}

问题是重定向没有出现在屏幕上,但是如果我在提交表单后打印$route_name,我仍然得到路由参数。

无论如何,目标是有一个带有 select 的表单来选择客户名称并将客户数据加载到 /stats/[name]。我走错方向了吗?重定向问题是错误还是我的代码有误?

所以我解决了它 rkeet,这是表格 & jquery:

<form id="customer_choice" method="POST" action=""> some form </form>
<script>
    $("#customer_choice").submit(function () {
        $("#customer_choice").attr('action', 'stats/' + $("#customer_select").val())
    });
</script>

这是控制器(希望没有客户被点名'none'):

$stat = new Stat();

$name = $this->params()->fromRoute('name', 'none');

if(!strcmp($name, 'none'))
{
    return new ViewModel([
        'customer_list' => $stat->get_customer_list(),
    ]);
}
else
{
    return new ViewModel([
        'avg_time' => $stat->get_avg_time($name),
    ]);
}

结果是 basepath/stats/[customer name] 并且手动更改 url 也可以。

(如果您不想手动更改 url 来更改结果,请使用 fromPost 而不是 fromRoute)