Forms Symfony2错误页包含表单(404)
我创建了一个自定义错误模板Forms Symfony2错误页包含表单(404),forms,symfony,error-handling,http-status-code-404,symfony-2.4,Forms,Symfony,Error Handling,Http Status Code 404,Symfony 2.4,我创建了一个自定义错误模板 app/Resources/TwigBundle/views/Exception/error404.html.twig 及 app/Resources/TwigBundle/views/Exception/Exception\u full.html.twig 为了开发,我想改变布局。这个很好用。但是,一旦我使用添加表单(例如我的搜索或联系人表单) {{include('mycorundle:Default/forms:myCustomForm.html.twig')}
app/Resources/TwigBundle/views/Exception/error404.html.twig
及
app/Resources/TwigBundle/views/Exception/Exception\u full.html.twig
为了开发,我想改变布局。这个很好用。但是,一旦我使用添加表单(例如我的搜索或联系人表单)
{{include('mycorundle:Default/forms:myCustomForm.html.twig')}
或者直接输入代码,它会显示ResourceNotFoundException
和NotFoundHttpException
,而不是404页面
有没有办法在错误模板上显示表单?使用错误模板怎么样
下面是从Symfony的文档页面中选取的简短示例
您有一个控制器,用于返回和呈现某些输出:
// src/Acme/ArticleBundle/Controller/ArticleController.php
class ArticleController extends Controller
{
public function recentArticlesAction($max = 3)
{
// make a database call or other logic
// to get the "$max" most recent articles
$articles = ...;
return $this->render(
'AcmeArticleBundle:Article:recentList.html.twig',
array('articles' => $articles)
);
}
}
在本例中,它渲染一个视图:
{# src/Acme/ArticleBundle/Resources/views/Article/recentList.html.twig #}
{% for article in articles %}
<a href="/article/{{ article.slug }}">
{{ article.title }}
</a>
{% endfor %}
{src/Acme/ArticleBundle/Resources/views/Article/recentList.html.twig}
{文章%中的文章为%s}
{%endfor%}
在您希望嵌入控制器动作的视图中:
{# app/Resources/views/base.html.twig #}
{# ... #}
<div id="sidebar">
{{ render(controller('AcmeArticleBundle:Article:recentArticles', {
'max': 3
})) }}
</div>
{app/Resources/views/base.html.twig}
{# ... #}
{{render(controller('AcmeArticleBundle:Article:recentArticles'{
“最大”:3
})) }}
“文档”页面还指出,当您需要模板中无法访问的数据时,应使用和嵌入控制器。在我看来,它非常适合创建小部件、一些统计数据等等
希望这有帮助
编辑
为了使此答案更准确地回答您的问题,我将使用以下解决方案:
SearchController
renderForm()
方法中,需要创建一个表单并将其传递给视图resultAction()
方法,您可以从数据存储中获取结果并将结果显示给最终用户