Symfony6 输入提交和从 twig 模板获取的路由

问题描述 投票:0回答:1

我正在 Symfony6 中工作,从只读端点获取信息。我正在重写一个现有但已严重弃用的应用程序,因此我确切地知道这些内容需要如何组合在一起才能更新检索到的内容。另外,我没有使用 Symfony 表单,因为这非常简单,并且没有存储任何内容或类似的内容。我的主页点击“/”效果很好,如果我将所有“?foo=bar&date=2023-11-16”复制并粘贴到最后,它会按照预期将正确的内容拉到正确的位置。但是我似乎无法弄清楚如何将模板中的信息获取到事物中并更新视图。

<div>
    <form class="class" role="form" action="this-is-the-question-i-think" method="get">
        <label for="foo" class="form-label">Foo</label>
        <input class="form-control" type="text" value="{{ bar }}">
        <label for="date" class="form-label">Date:</label>
<input type="date" class="form-control" id="theDate" value="{{ dateVariable }}">
        <button type="submit">Update View</button>
    </form>
</div>

我尝试了 action = "{{ path(path) }}" ,它填充了路由名称 'app_default_homepage' ,它加载没有默认值而不是输入的主页。我尝试过 action = "{{relative_path('/?foo=bar&date=2023-11-16') }}" 加载 '/?'但默认值(硬连线添加的内容,如果我什至可以让它加载带有添加到 url 的值的页面,那么我可能可以处理树枝扩展以从输入值创建 url 的字符串。我不知道我是否需要更改为向按钮添加 onsubmit,而不是使用操作来更新控制器中的变量值,然后重定向到我创建的任何字符串,但我的一些摸索引发了错误“无法获取 URL对于命名路线 '/?foo=bar&date=2023-11-16'" 或类似的,我已经没有意大利面条可以扔到墙上或新的措辞可以搜索了。

编辑以根据要求添加控制器代码:

#[Route('/', name: 'app_default_homepage')]
public function homepage(Request $request)
{
    $stats = null;
// this baseInit sets the date formatting, region is null on the homepage at first load, the after the ? has region=stuff that sets to entire repo
    $this->baseInit($request);

    if ($this->region == null) {
        $entireRepository = new Region("1", "Repository", "Entire Repository");
        $this->region = $entireRepository->getDisplayId();
// retrieveStatsForEntireRepository sets the $this->region and then passes to same retrieveStats as in the else below
        $stats = $this->statsService->retrieveStatsForEntireRepository(
            $this->fromDate,
            $this->toDate
        );
    } else {
        $stats = $this->statsService->retrieveStats(
            $this->region,
            $this->fromDate,
            $this->toDate
        );
    }
    return $this->render('homepage.html.twig', [
        'stats' => $stats,
    ]);
}

}

twig form-submit symfony6
1个回答
0
投票

在弄清楚这实际上并不是提交表单后,我将操作设置回 twig 模板中的 {{ path(path) }},将其一路带回到 w3schools php 表单处理页面,并将我的更改为

我假设更改输入而不是按钮是解决此特定问题的部分。继续下一篇。

© www.soinside.com 2019 - 2024. All rights reserved.