Symfony Routing - 可选参数

Ric*_*ich 4 php iphone json symfony1

我正在创建一个Symfony项目,该项目输出JSON以供iPhone App使用.

我有一个路线设置:

notification_json:
  url: /notifications.json
  param: { module: notification, action: index, sf_format: json }
Run Code Online (Sandbox Code Playgroud)

这当前列出了所有通知(我的一个模型).

我想要做的是添加一些参数,就像Twitter API有'since_id' - 所以如果请求/notifications.json?since_id=3,则只会显示ID大于3的通知.

我不能看到我在动作逻辑上挣扎,但是这种事情的路由让我有点挠头!

任何帮助将非常感激.

ric*_*age 6

如果:myparam在定义路径时未指定任何命名参数(例如)routing.yml,则在生成路径时添加的任何参数都将作为普通GET变量附加.例如:

notification_json:
  url: /notifications.json
  param: { module: notification, action: index, sf_format: json }
Run Code Online (Sandbox Code Playgroud)

用于:

<?php echo url_for("@notification_json?since_id=3"); ?>
Run Code Online (Sandbox Code Playgroud)

会给你:

/notifications.json?since_id=3
Run Code Online (Sandbox Code Playgroud)

如果您将命名参数添加到您的路线中,那么这些参数将被正常替换,并且如上所述在末尾标记任何其他参数,例如:

notification_json_test:
  url: /:param1/notifications.json
  param: { module: notification, action: index, sf_format: json }
Run Code Online (Sandbox Code Playgroud)

用于:

<?php echo url_for("@notification_json?param1=foo&since_id=3"); ?>
Run Code Online (Sandbox Code Playgroud)

会给你:

/foo/notifications.json?since_id=3
Run Code Online (Sandbox Code Playgroud)

  • ":参数1"到你的路线,并给它当u指定它这是overritten默认值:您还可以添加参数`notification_json:``网址:/:参数1/notifications.json``PARAM:{参数1:"" ,module:notification,action:index,sf_format:json}` (2认同)