cakePHP paginate with post data without sessions, serialization or post to get

前端 未结 5 812
难免孤独
难免孤独 2021-01-12 21:49

I have created a small search and filter form with a POST action in controller/index, which POSTs to itself the conditions and fields to paginate ($this->paginate($

相关标签:
5条回答
  • 2021-01-12 22:03

    Is there an un complex way to pass the post conditions to the other paginated pages?

    Nope.

    Is there an good cake way to do this more like passArgs, sessions and url encode do not look like cake style.

    There is only one way, no matter, cake or not cake.

    1. Search must be done using GET method.

    2. Parameters being passed via QUERY STRING.

    So, make your search form with method="GET" and then use http_build_query() to assemble a query string and use it to make links to other pages.

    Being a little curious, you can see an example right here on SO:

    http://stackoverflow.com/questions/tagged?tagnames=php&page=5&sort=newest&pagesize=50
    
    0 讨论(0)
  • 2021-01-12 22:12

    Thanks Deizel for the POST / REDIRECT / GET pattern.

    I implemented the GET method of posting data. For pagination used this
    $urlparams = $this->params['url'];unset($urlparams['url']); $paginator->options(array('url' => array('?' => http_build_query($urlparams))));

    We had multi checkboxes and the naming convention used where :
    foreach ($checkboxes as $chbox ) {

    // used variable variables to generate the data grid table based on selected fields
    ${'_field'.$chbox} = (isset($this->params['url']['displayfields'][$chbox])?$this->params['url']['displayfields'][$chbox]:1);
    
    $options = array('label'=>$chbox,'type'=>'checkbox','value'=> ${'_field'.$chbox});
    if ( ${'_field'.$chbox} ) $options['checked'] = 'checked';
    
    echo $form->input('Filter.displayfields['.$chbox.']',$options);
    



    In the post method the naming convention for the checkboxs would be Filter.displayfields.checkbox name

    This helps in getting an array either in $this->data or $this->params['url']

    There should be an persistent pagination plugin/component for cakePHP would make life much more easier and fun to develop with cakePHP.


    Thanks all.

    0 讨论(0)
  • 2021-01-12 22:17

    If it was me I would run with your idea of saving the stuff into the session. Then I would add a page dimension to the session, to store each page, thus allowing users to go back and forth with ease.

    $this->Session->write('Search.page.1.params',$params);
    $this->Session->write('Search.page.2.params',$params2);
    

    In order to do it in a Cake way, you'd probably want to write your own Pagination helper, or plugin. Which you could then use more effectivly in your controllers as

    $this->MyPages->paginate('MyModel');
    

    I suppose, this functionality would also give you the option to allow your users to 'Save my search' if they wanted to, as you could dump the session params into a SavedSearch model or similar.

    Don't forget to $this->Session->destroy() before starting a new search though!

    0 讨论(0)
  • 2021-01-12 22:22

    You can use passedArgs.

    in the method controller :

    if ( empty($this->passedArgs['search']) ){
        $this->passedArgs['search'] = $this->data['YourModel']['search'];
    }
    if ( empty($this->data) ){
        $this->data['YourModel']['search'] = $this->passedArgs['search'];
    }
    

    in your view :

    $this->Paginator->options(array('url' => $this->passedArgs));
    
    0 讨论(0)
  • 2021-01-12 22:22

    You can also use the Post/Redirect/Get design pattern pattern to solve this, allowing users to bookmark URLs of searches (without them expiring as a session would) and keeping URLs looking friendly. For example:

    function index($searchTerms = null) {
        // post/redirect/get
        if (isset($this->data['SearchForm'])) {
            $this->redirect(array($this->data['SearchForm']['search_terms']));
        }
        // your normal code here.
    }
    

    The search form data POSTs to /controller/action but the user is redirected and instead GETs /controller/action/search+terms where the terms are passed into the action as a parameter (ie. $searchTerms).

    If you simply change the form submission method to GET you will instead see something like: /controller/action?data[SearchForm][search_terms]=search+terms

    0 讨论(0)
提交回复
热议问题