PHP(CakePHP3.7.1)で商品の画像管理システムを作っています。
現在CakePHP3のPaginator機能を使用し商品画像一覧と検索機能を実装しているのですが、検索条件がGETパラメータで見えてしまう状態です。
今後アカウント管理機能も作成する予定で、メールアドレスなど個人情報が検索条件に含めれる想定なので、CakePHP3のPaginator機能を使用した上でGETパラメータなどで検索条件が見えない形で、検索を行った後に2ページ目に遷移しても検索条件が引き継がれたままで検索条件に該当するデータの2ページ目が正常に表示される実装方法を教えていただきたいです。
該当のソースコード
Template
<?= $this->Form->create(null, ['type'=>'get', 'url'=>['action'=>'index', $producttypeId]]) ?> <!-- 検索 --> <h4 class="function-name-color">・画像検索</h4> <div class="form-group input-group"> <span class="input-group-addon">キーワード</span> <?= $this->Form->input('keyword', ['type'=>'text', 'label'=>false, 'placeholder'=>'キーワード入力してください', 'class'=>'form-control', 'value'=>$keyword]) ?> <span class="input-group-addon">商品カテゴリ</span> <?= $this->Form->input('category_id', [ 'label'=>false, 'options'=>$categories, 'class'=>'form-control', 'templates' => ['inputContainer' => '{{content}}'], 'empty'=>'全てのカテゴリ', 'default'=>$categoryId, ]) ?> </div> <div class="text-center" style="margin-bottom:23px;"> <?= $this->Form->button(__('検索<i class="ti-search m-l-5"></i>'), ['id'=>'button', 'type'=>'submit', 'class'=>'btn btn-info p-l-20 p-r-20', 'div'=>false,'role'=>'button']) ?> </div> <?= $this->Form->end() ?>
Controller
/** * Index method * 初期表示・検索結果表示 */ public function index($producttypeId = null) { $this->loadModel('Categories'); $this->loadModel('Producttypes'); // 検索条件 $keyword = null; $categoryId = null; // 画像データ $materialData = null; if ($this->request->is('get')) { // 検索ファイル名 $keyword = $this->request->query('keyword'); // 検索カテゴリ $categoryId = $this->request->query('category_id'); } // 商品カテゴリプルダウンデータ取得 $categories = $this->Categories->find('list')->where(['producttype_id' => $producttypeId]); // 検索条件追加 $where_tmp = []; if ($keyword) { $where_tmp += ['OR' => [ 'Items.name like' => '%'. $keyword. '%', 'Items.description like' => '%'. $keyword. '%'] ]; } if ($categoryId) { $where_tmp += ['Items.category_id' => $categoryId]; } // CakePHPページング機能 $this->paginate = [ 'contain'=>['Categories'], 'conditions'=>['Categories.producttype_id' => $producttypeId, $where_tmp], 'order' => ['Items.id DESC'], 'limit'=>30, ]; $materialData = $this->paginate($this->Items); $this->set(compact('producttypeId', 'categories', 'materialData', 'keyword', 'categoryId')); }
試したこと
GETではなくPOSTで実装してみたのですが、検索条件を入力し検索結果一覧表示後に2ページ目に遷移すると、検索条件がリセットされてしまい、検索をかけていない全件表示状態の2ページ目が表示されてしまいました。
その為、検索条件がPOSTされた際に検索条件をsessionで保持し、2ページ目に遷移した時にsessionに保持されている条件でデータ抽出するようにしてみたのですが、sessionに検索条件が保持されている場合は無条件に検索をかけてしまうため、メニューから「商品の画像管理システム」のリンクを押下した時の初期表示のときも、sessionに検索条件が保持されている場合は検索をかけてしまう動きとなってしまいます。
初期表示の時はsessionに保持さている検索条件を破棄しようとしたのですが、メニューのリンクが押下された時の初期表示なのかPaginator機能の進むや戻るのリンクが押下された時の表示なのかがController側で識別ができず、POSTでsessionを使用しての検索条件を保持する実装は断念しました。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/04/15 09:18 編集
2019/04/16 00:07