How to have $this->redirect multiple times into single action? (I need to pass params)

I have two methods →

gerarPdfsel

and gerarPdf

When I click gerarPdfsel it will get id and pass as parameters

foreach…{
$this->redirect(array(‘controller’ => ‘ShowPayamentSlip’,‘action’ =>‘gerarPdf/’.$key));
}

but when I call the first $this->redirect, the next wont execute…

What are you trying to accomplish here? “Redirect” by its nature tells the browser that this page is done, and it should move on to a different one. If that’s not what you want to happen, then redirect is simply the wrong tool for the job. Maybe explain the high-level functionality that you’re trying to achieve?

1 Like

I’m trying to access a action that is on the same controller, I need it to pass a variable to be used with $this->request->params[‘pass’][0]

So what I’m trying to do is run the action gerarPdf(); multiple times while passing the param of each key
I’ve tried this $this->setAction(‘gerarPdf’,$key) but it didnt work.

I’ve tried $this->setAction(‘geraPdf’/.$key) but obviously didnt work because it tried to call gerarPdf/88();

I’m with @Zuluru, even with your description I’m not sure what your trying to have the system do or what you want the user to experience.

It sounds like this might be related to the previous page you were working on and this is now the process of downloading a set of PDFs to your user?

Or possibly you just want to create a set of PDFs (gerarPDF translates to generatePDF ?).

If you are downloading to the user, take a look here. The trick would be sending multiple files rather than a single.

If you are just generating the PDFs for storage, it is simply a matter of calling the method that does the work in a loop, providing the required params for each one.

Yes but I think you’re missing a point here, I’m not generating files with cake php itself, I’m using another lib that convert html to PDF and output it in the view in the shadow.
Also Im not generating PDFs for storage, but for the user download.

So I think that if I call the same method multiple times with different id, I can download multiple items at same time?

There’s two methods inside this lib ->output to browser and output download

Yes gerarPdf is generatePDF
So technically what i did is:

gerarPdf(){
if post{ 
->$id = $this->request->params['pass'][0]

->Controller load table registry where id = $id
-> set to the view
-> view load data and output as download.
}
}

The view itself doesnt load to the user because it needs to be “empty” so the htmltoPdf can work.

What I tried to do.

    foreach($this->request->getData('flag') as $key => $datum{
    if ($datum == 1 ) //if the checkbox is checked

    $this->redirect(array(‘controller’ => ‘ShowPayamentSlip’,‘action’ =>‘gerarPdf/’.$key));
    }

an possible solution i’m still trying is:

gerarPdf now receive a parameter inside its function with a list of all ids

gerarPdf($arrayid){

       ->Controller load table registry conditions array("payamentSlip.id" = $arrayid)
        -> set to the view
        -> view load data in foreach and output as download every item on the loop so user wil receive multiple downloads.
    }

    gerarPdfSel(){
    foreach($this->request->getData('flag') as $key => $datum{
        if ($datum == 1 ) //if the checkbox is checked

    array_push($data,$key);
        }
    $this->setAction('gerarPdf',$data):
    }```

//
gerarPdf = download item
gerarPdfSel = send data for which item user want to download.

Well I tried with set action, but I need wait the $this->setAction() finish before call another one, how it is possible?

The way you’re trying to do this is not going to work. A page can return one result, and one result only. If you return a document, you can’t also redirect to a second page. And you can’t return multiple documents.

There are ways to approximate this. See this discussion for some examples. But no solution involving redirect or setAction is going to work.

1 Like

I managed to make it to work I’ll share what I did

I used javascript to call window passing parameters. It is not a ideal solution but it works fine in my case to download multiple pdf’s


<script>
    $(document).ready(function() {
   var js_array = [<?php echo '"'.implode('","',$data).'"' ?>];
     var x  = js_array.length;
    document.getElementById("myText").innerHTML = x;
    //  alert(js_array);
    // function dos(){

    // };
    var i = 0;   
      for (i = 0; i < x; i++){
       
         window.open("http://localhost/sistemarbl/show-payament-slip/gerarPdf/" + js_array[i],'_blank');
         window.focus();
        //  setTimeout(f,3000);
        
      }

    });
</script>

In my controller I’ve set the $data to the view.
Converted my php array to javascript array
and open the action with param with window.open