Page linking breaks up search results based on post cakephp data

When I search for my listing, I get some paginated results, but when I go to the second page, my search since it was a receive request, when I get the search results through the post method.

Note. . To get the search results, I don’t want to send the form via the receive request (i.e. query string parameters), and I also do not want to save the form data in the session

Is there a way to get results that satisfy the above conditions?

+1
source share
3 answers

You want to implement a PRG template .

Post / Redirect / Get (PRG) is a web development design template that prevents some duplicate forms, creating a more intuitive interface for user agents (users). PRG implements bookmarks and update the button in a predictable way that does not duplicate the form.

CakeDC Search plugin makes this pretty easy to do in CakePHP.

+1
source

It would be very difficult to do this using only "POST" calls. You need to translate your POST into a GET call.

Mark this post made by me or clone it from github

Hope this helps

EDIT:

Using my git repository. If you want URL requests to be repeated instead of named parameters:

  • in this line , not foreach, build a chain and pass it to a redirect
  • in this line , get the parameters from the query string ($ GET)
  • and in this line add page , sort and direction to this->paginate

I have not tested it, but it should be something like this

0
source

We can do this with a patch.

In views:

create a search form:

 $this->Form->create('Search', array('url' => array('controller' => 'controller', 'action' => 'index', substr(time(), 2,rand(1, 7) ))) ); 

Note. A random number added at the end of the form action. This will tell us when to clear the session.

in the controller:

  public function index( $search = null) { $conditions = array(1 => 1); if( !empty($this->data['Search']['keyword']) && $search) { $conditions = array('Model.field like' => $this->data['Search']['keyword'] . '%'); // store search array in session $this->Session->write('conditions', $this->data['Search']); } if ($search) { $this->request->data['Search'] = $this->Session->read('conditions'); $conditions = array('Model.field like' => $this->data['Search']['keyword'] . '%'); } else { $conditions = array(1 => 1); $this->Session->delete('conditions'); } $this->paginate= array('limit'=> 10, 'conditions' => $conditions); $lists = $this->Paginate('Model'); } 

I hope you understand the logic.

0
source

Source: https://habr.com/ru/post/1444413/


All Articles