Use the "bare" param option in CakePHP - cakephp-2.0

I am setting a post request. However, I noticed that instead of getting back plain data i am getting back an entire webpage with default layout and corresponding view?
What I want to know is how to set the CakeResponse "params" default options. Specifically the 'bare' one so that whenever I do a post it just returns data, not a view / layout.
Thanks

$this->autoRender = false;
You do not need a view folder, file or anything. Great for just responding with plain json / text / etc.

Related

How to process and change output before it is sent back to the browser?

I want to implement a mechanism in CakePHP that is like ShortCodes in Wordpress. I want to save my pages (in the DB) with tokens, e.g.
[form id=12]
and then, after the view got rendered and before it is sent back to the browser, I want to search the rendered view for these tokens, and replace them with something else.
I assume I'll have to use beforeFilter, afterFilter or beforeRender, but I can't find any documentation (including in CakePHP's own documentation!) about how these overriden functions can be used to change the output.
Can anyone help?

Cakephp: iterate through pages in a custom view

I have a view with several megabytes of data and I expect it to grow radically.
Controller index function is implemented in a default way, with pagination.
I would like to export this view to a csv without making much changes to the controller ( I'm fine to define header rows and rows to be included in a CSV but not to remove pagination as it brakes the html representation ).
The idea is simple: render csv view template, change the page, render another one. But how can I change current pagination settings in a custom view?
PS: I did take a look at the csv plugin. It doesn't work with pagination so I get out of the memory limits, it also creates a tmp file, I prefer to stream content on the fly.
I wouldn't use the paginator here, just get the total amount of records then do a while() loop and fetch the data in batches to avoid memory limitations. And send it as it comes from the DB directly to the client. Use the HTTP Client that comes with CakePHP and set the proper header properties.
See these two answers how to send it as stream:
send a file to client
PHP: stream remote pdf to client browser
Streaming a large file using PHP

Get View Output In CakePHP 2.x Model

Context: I'm generating a PDF in a model callback (afterSave). I found a library I'm comfortable with called FPDF. I'm adding the FPDF class as a vendor.
So, without going into too much detail, essentially, once all checks have been completed for a particular contract application, the app needs to prepopulate a PDF file and attach it to an email.
I can figure everything out except how to generate the PDF in the model. I want to use a view to pass view vars to so that I can populate the template file and use the FPDF class to save a PDF file.
This file will in turn be attached the automated email and sent to the applicant.
So the flow is:
Once all checks have been complete (via crons and a CakePHP Shell), we trigger a function inside the model afterSave callback.
The function performs some logic and determines whether a declined or approved email should be sent.
If approved, then a PDF is generated using a view file and saved in /Views/pdf/
This file is attached to CakeEmail object and sent.
It's just the view rendering part here that I'm stuck with:
3. If approved, then a PDF is generated using a view file and saved in /Views/pdf/
How can I populate a view file with view variables and return the result into a variable?
For example, think of how the CakeEmail class does it with the CakeEmail->template('example') function......
Any ideas?
The answer is to construct your view class manually:
$view = new View(null, false);
$view->set(compact('variable1', 'variable2'));
$view->viewPath = 'ViewFolder';
$output = $view->render('view_file', 'layout');

CakePHP: Use current parameters to render another view

I have a searchable, sortable table in cakephp. The search and sort criteria are passed as named parameters. Now I would like to add a button which calls a method viewPdf on the very same controller to create a PDF showing the current table content. In fact, I get the correct PDF output just by replacing the action parameter in my url. But how do I get the correct link url for the button with all the current parameters?
I could implode all the values and keys of $this->params->named but I am sure there is a much better way to achieve what I want.
Regards
Alex
You are actually pretty close. You can form such a link using
$url = array('action' => 'pdf') + $this->request->params['named'];
echo $this->Html->link('Title', $url);
Assuming you are using cake2.x (which you always should mention in your question!).

Ext JS ComboBox Dynamic Behaviour

I am trying to load/show completely different set of values in a combobox(this one resides as a editor within an EditorGridPanel) based on the valueField of another combobox(this one resides outside the grid in top bar). I have already seen a tutorial(http://www.extjs.com/learn/Tutorial%3ALinked%5FCombos%5FTutorial%5Ffor%5FExt%5F2) wherein ALL the values for the secondary object are stored locally and then filtered however, I have already created a link which will supply me with json data based on the valuefield, so I would like to use this url to keep the code efficient.
I have also tried to refresh the datastore but its simply not being reflected on the combobox.
Please advise
Thanks
Found the solution, loading values from a url is straightforward.. if you want to manipulate the query(like I wanted), you would have to strip url off the dynamic parameters and assign them to baseParams of the store and then call store.load()
It worked for me!!

Resources