Some mandatory parameters are missing エラーが解決できません

622 views
Skip to first unread message

田中宏典

unread,
Oct 4, 2016, 5:32:20 AM10/4/16
to 日本Symfonyユーザー会
書籍「基本からしっかり学ぶ Symfony2入門」を読みながらSymfonyの勉強をしています。

テキストどおりにコーディングしていたら次のエラーが発生しました。
An exception has been thrown during the rendering of a template ("Some mandatory parameters are missing ("format") to generate a URL for route "app_admininquirylist_index".") in Admin/Common/menu.html.twig at line 2.

パラメータ不足のエラーのようなので、menu.html.twigのエラー該当箇所を次のように変更してみましたが、同じエラーが発生します。
どうすればエラーを解消できるでしょうか?
【変更前】
    <li><a href="{{ path('app_admininquirylist_index' ) }}">お問い合わせ管理</a></li>
【変更後】
    <li><a href="{{ path('app_admininquirylist_index', {'_format':'html'}) }}">お問い合わせ管理</a></li>

【環境】
Symfony version 3.1.4

【ソース】
■menu.html.twig
<ul class="nav nav-sidebar">
    <li><a href="{{ path('app_admininquirylist_index' ) }}">お問い合わせ管理</a></li>
    <li><a href="">ブログ管理</a></li>
</ul>

■AdminInquiryListController.php
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\Form\Extension\Core\Type\SearchType;
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use AppBundle\Entity\Inquiry;
use League\Csv\Writer;
/**
 * @Route("/admin/inquiry")
 */
class AdminInquiryListController extends Controller
{
    /**
     * @Route("/search.{format}",
     *      defaults={"_format": "html"},
     *      requirements={
     *          "_format": "html|csv",
     *      }
     *  )
     */
    public function indexAction(Request $request, $_format)
    {
        $form = $this->createSearchForm();
        $form->handleRequest($request);
        $keyword = null;
        if ($form->isValid()) {
            $keyword = $form->get('search')->getData();
        }
        $em = $this->getDoctrine()->getManager();
        $inquiryRepository = $em->getRepository('AppBundle:Inquiry');
        $inquiryList = $inquiryRepository->findAllByKeyword($keyword);
        if ($_format == 'csv') {
            $response = new Response($this->createCsv($inquiryList));
            $d = $response->headers->makeDisposition(
                ResponseHeaderBug::DISPOSITION_ATTACHMENT,
                'inquiry_list.csv'
            );
            $response->headers->set('Content-Disposition', $d);
            return $response;
        }
        return $this->render('Admin/Inquiry/index.html.twig',
            [
                'form' => $form->createView(),
                'inquiryList' => $inquiryList
            ]
        );
    }
    public function createSearchForm()
    {
        return $this->createFormBuilder()
            ->add('search', SearchType::class)
            ->add('submit', ButtonType::class, [
                'label' => '検索',
            ])
            ->getForm();
    }
    private function createCsv($inquiryList)
    {
        /** @var Writer $writer */
        $writer = Writer::createFromString('','');
        $writer->setNewLine("\r\n");
        foreach ($inquiryList as $inquiry) {
            /** @var Inquiry $inquiry */
            $writer->insertOne([
                $inquiry->getId(),
                $inquiry->getName(),
                $inquiry->getEmail()
            ]);
        }
        return (string)$writer;
    }
}

■index.html.twig
{% extends "Admin/layout.html.twig" %}
{% block title %}お問い合わせ管理{% endblock %}
{% block body %}
<h2 class="sub-header">お問い合わせ管理</h2>
<div>
{{ form_start(form, {'attr': {'id':'searchForm', 'novalidate': 'novalidate'}}) }}
検索:{{ form_widget(form.search) }}
{{ form_widget(form.submit, {"attr": {"class":"btn btn-default searchButton"}}) }}
{{ form_end(form) }}
<a class="btn btn-default downloadButton">CSVダウンロード</a>
</div>
<table class="table table-striped">
    <thread>
    <tr>
        <th>#</th>
        <th>日時</th>
        <th>名前</th>
        <th>種別</th>
        <th>操作</th>
    </tr>
    </thread>
    <tbody>
    {% for inquiry in inquiryList %}
    <tr>
        <td>{{ inquiry.id }}</td>
        <td></td>
        <td>{{ inquiry.name }}</td>
        <td>{% if inquiry.type == 0 %}公演について{% else %}その他{% endif %}</td>
        <td>
            <a class="btn btn-default"
                href="{{ path("app_admininquiryedit_input", {"id": inquiry.id}) }}">詳細</a>
        </td>
    </tr>
    {% endfor %}
    </tbody>
</table>
{% endblock %}
{% block javascripts %}
{{ parent() }}
<script type="text/javascript">
$('.downloadButton').click(function() {
    $('#searchForm').attr('action', '{{ path(
        "app_admininquirylist_index", {"_format":"csv"}) }}');
    $('#searchForm').submit();
});
$('.searchButton').click(function() {
    $('#searchForm').attr('action', '{{ path(
        "app_admininquirylist_index", {"_format":"html"}) }}');
    $('#searchForm').submit();
});
</script>
{% endblock %}

田端孝至

unread,
Oct 4, 2016, 5:59:25 AM10/4/16
to symfony-...@googlegroups.com
formatというパラメータがありませんというエラーです。

> /**
> * @Route("/search.{format}",
> * defaults={"_format": "html"},
> * requirements={
> * "_format": "html|csv",
> * }
> * )
> */

{format}と書かれているところを{_format}に変更したら動くと思います。


On 2016/10/04 18:32, 田中宏典 wrote:
> 書籍「基本からしっかり学ぶ Symfony2入門」を読みながらSymfonyの勉強をして
> います。
> --
> --
> -------------------
> Symfonyに関する疑問・質問、ユーザー会の活動やサイトに対するご意見、その
> 他雑談など、お気軽にMLへ投稿してください!
>
>
> 日本Symfonyユーザー会
> http://www.symfony.gr.jp/
>
> 日本Symfonyユーザー会メーリングリスト
> http://groups.google.com/group/symfony-users-ja
>
> ---
> このメールは Google グループのグループ「日本Symfonyユーザー会」に登録し
> ているユーザーに送られています。
> このグループから退会し、グループからのメールの配信を停止するには
> symfony-users-...@googlegroups.com
> <mailto:symfony-users-...@googlegroups.com> にメールを送信し
> てください。
> その他のオプションについては https://groups.google.com/d/optout にアクセ
> スしてください。

田中宏典

unread,
Oct 4, 2016, 7:32:56 PM10/4/16
to 日本Symfonyユーザー会
takashi tabata さま、ありがとうございます。
アンダースコアが抜けていたのですね!
修正したらうまく動作するようになりました。

Reply all
Reply to author
Forward
0 new messages