簡體   English   中英

Symfony獲取帶有空格和下划線的url參數

[英]Symfony get url parameters with spaces and underscore

我是Symfony的新手,正在嘗試使用搜索字段過濾表。 我正在使用KnpPaginatorBundle對表進行分頁和排序,並且創建了一個表單來過濾我的請求(方法GET)

它通常可以正常工作,但是當我在搜索輸入中使用空格或下划線時,它不起作用,我認為與GET方法和編碼文本的方式有關,但我不知道該怎么做。

這是我的代碼:

查看:

<div class="row">
        <div class="well row">
            <form action="" method="get">

                <div class="col-md-2">
                    <label for="famille">Famille d'articles</label>
                    <select name="famille">
                        <option value="0">Toutes</option>
                        {% for famille in listFamilles %}
                            <option value="{{ famille.id }}" {% if data.famille is defined %} {% if famille.id == data.famille %} selected {% endif %} {% endif %}>{{ famille.nom }}</option>
                        {% endfor %}
                    </select>
                </div>

                <div class="col-md-4">
                    <input type="checkbox" name="rds" {% if data.rds == 1 %} checked {% endif %}>
                    <label for="rds" style="margin-left:5px">Montrer les articles en rupture de stock</label>
                </div>

                <div class="col-md-4">
                    <label for="recherche">Recherche</label>
                    <input name="recherche" style="width:100%" type="text" placeholder="Recherche" {% if data.recherche is defined %} value="{{ data.recherche }}" {% endif %}>
                </div>

                <div class="col-md-2" style="text-align:center">
                    <button type="submit" class="btn btn-primary">Rechercher</button>
                </div>

            </form>
        </div>
        <div class="well row">
            <table class="table table-bordered table-striped" style="width: 100%" cellspacing="0">
                <thead>
                    <tr>
                        <th>{{ knp_pagination_sortable(listArticles, 'Référence client', 'a.ref_article') }}</th>
                        <th>{{ knp_pagination_sortable(listArticles, 'Référence interne', 'a.ref_logistique') }}</th>
                        <th>{{ knp_pagination_sortable(listArticles, 'Famille', 'f.nom') }}</th>
                        <th>{{ knp_pagination_sortable(listArticles, 'Libellé', 'a.libelle') }}</th>
                        <th>{{ knp_pagination_sortable(listArticles, 'Alerte', 'a.stock_alerte') }}</th>
                        <th>{{ knp_pagination_sortable(listArticles, 'Stock', 'a.stock_actuel') }}</th>
                    </tr>
                </thead>
                <tbody id="bodyListeArticles">
                    {% for article in listArticles %}
                        <tr>
                            <td><a href="{{ path('gr_bo_modif_article', {'article_id': article.id}) }}">{{ article.refArticle }}</a></td>
                            <td>{{ article.refLogistique }}</td>
                            <td>{{ article.famille.nom }}</td>
                            <td>{{ article.libelle }}</td>
                            <td>{{ article.StockAlerte }}</td>
                            <td>{{ article.StockActuel }}</td>
                        </tr>
                    {% endfor %}
                </tbody>
            </table>

            <div class="navigation text-center">
                {{ knp_pagination_render(listArticles) }}
            </div>

        </div>

    </div>

控制器:

public function listeAction(Request $request) {
        if ($this->get('security.authorization_checker')->isGranted('ROLE_OPERATEUR')) {
            $session = $request->getSession();
            if ($session->get('client_id')) {
                $clientId = $session->get('client_id');
            } else {
                $request->getSession()->getFlashBag()->add('info', 'Vous devez sélectionner un client pour accéder à la liste de ses articles.');
                return $this->redirectToRoute('gr_bo_liste_clients');
            }
        } elseif ($this->get('security.authorization_checker')->isGranted('ROLE_SUPERCOLLABORATEUR') || ($this->get('security.authorization_checker')->isGranted('ROLE_COLLABORATEUR') && $this->getUser()->getListeArticles())) {
            $clientId = $this->getUser()->getClient()->getId();
        } else {
            $request->getSession()->getFlashBag()->add('info', 'Vous n\'avez pas les permissions requises pour accéder à cette page.');
            return $this->redirectToRoute('gr_bo_liste_commandes');
        }
        $em = $this->getDoctrine()->getManager();
        $data = [];
        $data['clientId'] = $clientId;

        if ($request->query->getAlnum('recherche')) {
            $data['recherche'] = $request->query->getAlnum('recherche');
        }

        if ($request->query->getAlnum('famille') && $request->query->getAlnum('famille') != "0") {
            $data['famille'] = $request->query->getAlnum('famille');
        }


        if ($request->query->getAlNum('rds') == "on" || ($request->query->getAlnum('rds') == "" && $request->query->getAlnum('famille') == "" && $request->query->getAlnum('recherche') == "")) {
            $data['rds'] = 1;
        } else {
            $data['rds'] = 0;
        }

        $listArticles = $em->getRepository('GRBackOfficeBundle:Article')->getQueryArticles($data);

        /**
         * @var $paginator \Knp\Component\Pager\Paginator
         */
        $paginator = $this->get('knp_paginator');
        $result = $paginator->paginate(
                $listArticles, $request->query->getInt('page', 1), $request->query->getInt('limit', 5)
        );

        $listFamilles = $em->getRepository('GRBackOfficeBundle:Famille')->findAll();

        return $this->render('GRBackOfficeBundle:Article:liste_articles.html.twig', array(
                    'listArticles' => $result,
                    'listFamilles' => $listFamilles,
                    'data' => $data
        ));
    }

倉庫:

public function getQueryArticles($data) {
        $query = $this->createQueryBuilder('a')
                ->leftJoin('a.images', 'i')
                ->addSelect('i')
                ->leftJoin('a.type_stockage', 't')
                ->addSelect('t')
                ->leftJoin('a.famille', 'f')
                ->addSelect('f');
        if (array_key_exists('famille', $data)) {
            $query->andWhere('f.id = :famille')
                    ->setParameter('famille', $data['famille']);
        }
        if (array_key_exists('rds', $data)) {
            if ($data['rds'] == 0) {
                $query->andWhere('a.stock_actuel > 0');
            }
        }
        if (array_key_exists('recherche', $data)) {
            $query->andWhere('a.ref_article LIKE :recherche OR a.ref_logistique LIKE :recherche OR a.libelle LIKE :recherche')
                    ->setParameter('recherche', '%' . $data['recherche'] . '%');
        }
        $query->leftJoin('a.sousfamille', 's')
                ->addSelect('s')
                ->leftJoin('a.client', 'c')
                ->addSelect('c')
                ->andWhere('c.id = :client')
                ->setParameter('client', $data['clientId'])
                ->orderBy('a.ref_article', 'ASC')
                ->getQuery();


        return $query;
    }

當我在“ recherche”搜索過濾器中使用空格或下划線時,我的表顯示為空,並且似乎刪除了空格或下划線,因此如果我嘗試使用“ John Doe”或“ John_Doe”,它將返回我結果為“ JohnDoe”,為空。

如果有人對我的工作方式有所了解,將不勝感激!

您可以在data.recherche上使用urlencode 但是在樹枝中也有更自然的方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM