代码之家  ›  专栏  ›  技术社区  ›  Daniel Boling

控制器参数名称不变

  •  0
  • Daniel Boling  · 技术社区  · 1 年前

    |捆绑版本|5.9.0 |组件版本|3.6.0 |Symfony版本|6.4.2 |PHP版本|8.3.2

    我在一个页面上有几个分页对象,所以我想更改排序和分页的相应参数名称,这样它们就不会冲突。分页通过使用“pageParameterName”来工作(有关更多详细信息,请参阅下面的代码),但使用文档中的“sortFieldParameterName”和“SortDirectionParameterName”不会改变任何内容。参数仍然是默认的,我不想在应用程序的其他部分更改,只想在这个函数中更改。

    位置表函数,它调用表的初始页面,Twig在其中嵌入下面的项表。

      /**
       * Function to display all locations in the system
       * 
       * @author Daniel Boling
       */
      #[Route('/location/list/', name:'loc_list')]
      public function loc_list(Request $request): Response
      {
        if (isset($request->query->all()['loc_code']))
        {
          $loc = $this->loc_repo->find($request->query->all()['loc_code']);
        } else {
          $loc = new Location;
        }
        $loc_form = $this->createForm(LocationType::class, $loc);
        $loc_thead = [
          'loc_code' => 'Loc Code',
          'loc_desc' => 'Loc Desc',
          'loc_notes' => 'Loc Notes',
          'item_total_qty' => 'Item Total Qty.',
        ];
        $result = $this->loc_repo->findAll();
        $result = $this->paginator->paginate($result, $request->query->getInt('loc_page', 1), 1, ['sortFieldParameterName' => 'loc_sort', 'sortDirectionParameterName' => 'loc_direction', 'pageParameterName' => 'loc_page']);
        $normalized_locations = [];
        foreach ($result->getItems() as $item)
        {
          $normalized_locations[] = [
            'loc_code' => $item->getLocCode(),
            'loc_desc' => $item->getLocDesc(),
            'loc_notes' => $item->getLocNotes(),
            'item_total_qty' => $item->getItemQty(),
          ];
        }
        $result->setItems($normalized_locations);
    
        return $this->render('location/loc_list.html.twig', [
          'locations' => $result,
          'loc_thead' => $loc_thead,
          'form' => $loc_form,
        ]);
      }
    

    项目表片段。使用Twig控制器嵌入调用。

      /**
       * Fetches selected location's items for template fragment
       * 
       * @author Daniel Boling
       */
      public function loc_items_list(Request $request, ?string $loc_code, ?int $item_page = 1): Response
      {
        $loc = $this->loc_repo->find($loc_code);
        $item_thead = [
          'item_code' => 'Item Code',
          'item_desc' => 'Item Desc',
          'item_unit' => 'Item Unit',
          'item_notes' => 'Item Notes',
          'item_exp_date' => 'Item Exp. Date',
          'item_qty' => 'Item Total Qty.',
        ];
        // to autofill form fields, or leave them null.
        $result = $this->item_repo->findByLoc($loc_code);
        $result = $this->paginator->paginate($result, $item_page, 10, ['pageParameterName' => 'item_page', 'sortParameterName' => 'item_sort', 'sortDirectionParameterName' => 'item_direction']);
        $normalized_items = [];
        foreach ($result->getItems() as $item)
        {
          $normalized_items[] = [
            'item_code' => $item->getItemCode(),
            'item_desc' => $item->getItemDesc(),
            'item_notes' => $item->getItemNotes(),
            'item_exp_date' => $item->getItemExpDate(),
            'item_qty' => $item->getItemQty(),
            'item_unit' => $item->getItemUnit()->getUnitCode(),
          ];
        }
        $result->setItems($normalized_items);
        return $this->render('location/item_table.html.twig', [
          'items' => $result,
          'item_thead' => $item_thead,
        ]);
      }
    

    在我看来,每个表页面参数都应该有各自的名称,这是有效的。排序参数也应该以完全相同的方式更改,但它们不是。我错过了什么?

    0 回复  |  直到 1 年前