【问题标题】:Transform GET parameters to clean URL将 GET 参数转换为干净的 URL
【发布时间】:2016-12-21 17:10:53
【问题描述】:

我在前端使用 Datatables 将 GET 参数发送到我的 Silex 应用程序。

数据表发送该类型的 GET 参数:

champs_societes%5B%5D=naf&zone-geographique=ville&effectif%5B%5D=eff_1a9&effectif%5B%5D=eff_10a19&effectif
%5B%5D=eff_20a49&effectif%5B%5D=eff_plus5000&ca%5B%5D=10k-50k&ca%5B%5D=50k-100k&ca%5B%5D=1kk-2kk&ca%5B
%5D=2kk-5kk&champs_societes%5B%5D=capital_int&fondation%5Bmin%5D=&fondation%5Bmax%5D=&champs_societes
%5B%5D=siren&champs_societes%5B%5D=siret&champs_societes%5B%5D=nature&nature%5B%5D=Etablissement&champs_societes
%5B%5D=formejur&champs_societes%5B%5D=emailg&champs_contacts%5B%5D=emailn&ac_formejur=Artisan-Commer
%C3%A7ant%2CBanque+Populaire%2FLoi+Mars+1917%2CCoop.+%C3%80+Responsabilit%C3%A9+Limit%C3%A9e&ac_naf=0113Z
%2C0121Z%2C0126Z%2C0130Z&ac_departements=14%2C50%2C61%2C68%2C03&ac_villes=77330%2C77680%2C77340&ac_fonction
=Assistant%2CCharg%C3%A9+D'Affaires%2CContr%C3%B4leur+De+Gestion%2CDirecteur+%2F+Responsable

我有办法从这个链中生成一个干净的 URL 吗?理想情况下使用 Symfony/Silex 路由。

感谢您的帮助

编辑

我通过请求获得了上面的 GET 参数:

$app->post('/ajax/formprocess', function (Request $request) use ($app) {

    $df = new Filtres( $request->request->get('dataForm') );

    $filtroAdd = $df->getRequest();

【问题讨论】:

  • 我编辑了我的答案,我相信你的问题毕竟与 Request 无关,只是字符串解析

标签: symfony routing datatables silex


【解决方案1】:

我会先尝试使用 Request 类

Request 来自 HttpFoundation 组件的类(默认在 Symfony 中,不确定 Silex,因为我从未使用过它)

/**
 * @param \Symfony\Component\HttpFoundation\Request $request
 */
public function someAction(Request $request)
{
    $request->getSchemeAndHttpHost();
    $request->getBasePath();
    $request->getQueryString(); // this will be the most helpful in your case

    // access what you need and build normalized url
}

您应该能够构建干净的规范化网址

编辑,将查询参数字符串解析为数组的解决方案

$queryParameters = 'query parameters as string to be parsed';
$output = [];

parse_str($queryParameters, $queryParameters);

print_r($queryParameters);

http://php.net/manual/en/function.parse-str.php

【讨论】:

  • 我在控制器中使用 $request->get('dataForm') 获得了上面的 GET 链参数。但是 getSchemeAndHttpHost、getBasePath、getQueryString 方法都没有返回内容。 getQueryString('dataForm') 返回 NULL
  • 如果getQueryString() 没有返回任何内容,那么我认为您没有正确使用路由/控制器/请求。您能否包含代码 sn-p 您是如何阅读的?
  • @Macbernie 我有一种糟糕的感觉,这些不是您尝试读取的 GET 查询参数,而是 POST 正文。在问题中你提到了 GET。您应该搜索将带有转义查询参数的字符串转换为数组的解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-04
  • 1970-01-01
  • 1970-01-01
  • 2011-03-18
  • 1970-01-01
相关资源
最近更新 更多