Twig模板找不到id:“缺少一些必需参数(”id“)

时间:2015-12-10 11:22:18

标签: symfony twig

我找不到错误的原因:

在呈现模板期间抛出异常(“缺少一些必需参数(”id“)以生成路径”event_show“的URL。”)在MeetingBundle中:事件:ev_index.html.twig at line 25.

C:\ Bitnami \ wampstack-5.5.30-0 \ sym_prog \ proj2_27 \ SRC \ MeetingBundle \控制器\ EventController.php

/**
 * Event controller.
 *
 * @Route("/event")
 */
class EventController extends Controller
{

    /**
     * Lists all Event entities.
     *
     * @Route("/", name="event_index")
     * @Method("GET")
     * @Template("MeetingBundle:Event:ev_index.html.twig")
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $entities = $em->getRepository('MeetingBundle:Event')->findAll();

        dump($entities); //there are 19 lines in the table

        return array(
            'events' => $entities,
        );
    }

C:\ Bitnami \ wampstack-5.5.30-0 \ sym_prog \ proj2_27 \ SRC \ MeetingBundle \资源\视图\事件\ ev_index.html.twig

{% block body %}
    <h1>Event list</h1>
    <table>

            {# empty array evaluates to false, http://twig.sensiolabs.org/doc/tags/if.html #}
    {% if events is not null %}
        {% for event in events %}
            <tr>
                {% if event.id is not null %}  
                    <td><a href="{{ path('event_show', { 'event_id': event.id }) }}">{{ event.id }}</a></td> 
                {% endif %}

                <td>{{ event.title }} </td>
...
       {% endfor %}
 {% endif %}
    </table>
{% endblock %}

功能转储结果为:

array:19 [▼
  0 => Event {#1212 ▼
    -id: 1
    -title: "title1"
    -keywords: ""
    -starttime: DateTime {#1209 ▶}
    -endtime: null
    -details: null
    -address: ""
    #latitude: null
    #longitude: null
    #street: null
    #number: null
    #city: null
    #country: null
    #state: null
    #zip: null
    #admins: PersistentCollection {#1219 ▶}
    #attendees: PersistentCollection {#1232 ▶}
    #comments: PersistentCollection {#1275 ▶}
  }
  1 => Event {#1278 ▶}
  2 => Event {#1286 ▶}
  3 => Event {#1294 ▶}
  4 => Event {#1302 ▶}
  5 => Event {#1310 ▶}
  6 => Event {#1318 ▶}
  7 => Event {#1326 ▶}
  8 => Event {#1334 ▶}
  9 => Event {#1342 ▶}
  10 => Event {#1350 ▶}
  11 => Event {#1358 ▶}
  12 => Event {#1366 ▶}
  13 => Event {#1374 ▶}
  14 => Event {#1382 ▶}
  15 => Event {#1390 ▶}
  16 => Event {#1398 ▶}
  17 => Event {#1406 ▶}
  18 => Event {#1414 ▶}
]

2 个答案:

答案 0 :(得分:0)

错误很简单。

show动作中没有event_id参数。我应该使用id代替。

而不是&#34; <td><a href="{{ path('event_show', { 'event_id': event.id }) }}">{{ event.id }}</a></td>&#34;必须有&#34; <td><a href="{{ path('event_show', { 'id': event.id }) }}">{{ event.id }}</a></td>&#34;,

因为event_show路径适用于public function showAction($id)

答案 1 :(得分:0)

错误消息是关于id变量而不是event_id。可能是一个错字,所以试试这个:

                <td><a href="{{ path('event_show', { 'id': event.id }) }}">{{ event.id }}</a></td> 

而不是:

                <td><a href="{{ path('event_show', { 'event_id': event.id }) }}">{{ event.id }}</a></td> 

希望这个帮助