Symfony2在错误页面上包含表格(404)

Chr*_* We 2 forms error-handling http-status-code-404 symfony symfony-2.4

我创建了一个自定义错误模板

app/Resources/TwigBundle/views/Exception/error404.html.twig

app/Resources/TwigBundle/views/Exception/exception_full.html.twig

用于开发以将布局更改为我想要的内容.这很好用.但是,只要我使用添加表单(例如我的搜索或联系表单)

{{ include ('MyCoreBundle:Default/forms:myCustomForm.html.twig' ) }}

或直接输入代码,显示a ResourceNotFoundException和a NotFoundHttpException而不是404页面.

有没有办法在错误模板上显示表单?

And*_*ski 5

那么使用嵌入式控制器呢?

以下是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)
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

在此示例中,它呈现一个视图:

{# src/Acme/ArticleBundle/Resources/views/Article/recentList.html.twig #}
{% for article in articles %}
    <a href="/article/{{ article.slug }}">
        {{ article.title }}
    </a>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

在视图中你想嵌入控制器的动作:

{# app/Resources/views/base.html.twig #}

{# ... #}
<div id="sidebar">
    {{ render(controller('AcmeArticleBundle:Article:recentArticles', {
        'max': 3
    })) }}
</div>
Run Code Online (Sandbox Code Playgroud)

文档页面还指出,当您需要模板中无法访问的数据时,应使用嵌入式控制器.从我的角度来看,它非常适合创建小部件,一些统计数据等.

希望这可以帮助.

编辑

为了使您的问题更准确,这里是我使用的解决方案:

  1. 你需要一个控制器,让我们来称呼它 SearchController
  2. 在控制器的renderForm()方法中,您需要创建一个表单并将其传递给视图
  3. Form的action属性应指向resultAction()可以从数据存储中获取结果并将结果显示给最终用户的方法.