saving the xml file generated through cakephp 2.3 serialize function - cakephp

I am using cakephp 2.3 and using the default code from the cookbook. The xml is generated automatically, without having to create any view files.
class PostsController extends AppController {
public function index() {
$this->set(’posts’, $this->paginate());
$this->set(’_serialize’, array(’posts’));
}
}
However, I do not want to display the XML. Instead I want to save the generated XML file in the document root upon click of a button as Posts.xml. How can I do this? Please help.

You might not have look thoroughly enough:
http://book.cakephp.org/2.0/en/controllers/request-response.html#sending-files
It clearly states how to send files with the appropriate headers via response object.
So in your controller action, add:
$this->response->download('filename_for_download.xml');

Related

Cache View in CakePHP 3.0

I understand that we can cache an action or view page in CakePHP 2.0. Refer to this link http://book.cakephp.org/2.0/en/core-libraries/helpers/cache.html.
class PostsController extends AppController {
public $helpers = array('Cache');
}
public $cacheAction = array(
'view' => 36000,
'index' => 48000
);
However, it seems that CakePHP 3.0 has removed helper(http://book.cakephp.org/3.0/en/core-libraries/caching.html). Is there any other way in CakePHP 3.0 I can cache the view page. For example, I have a index view/action. And I would like to cache that page. Thanks.
Is there any other way in CakePHP 3.0 I can cache the view page.
Not without developing something yourself. Like for example saving the rendered content to a file in callback for Dispatcher.afterDispatch event and then checking for the file in callback for Dispatcher.beforeDispatch event and returning the cached response.
Better to use you something like Varnish which is more suited for the job.

How to create a link that download a file in Symfony

I did some research and I found this post:
How to create a link to download generated documents in symfony2?
I tried the solution, but it show my pdf in the browser, but what I want is that when someone click the link, it directly download the file. Is ther a way to do that with Symfony?
Kévin Duguay
Set up an action.
This uses annotation for the route. YOu can of course use yml or xml or whatever you are currently using
/**
* #Route("/download", name="download_file")
**/
public function downloadFileAction(){
$response = new BinaryFileResponse('path/to/pdf.pdf');
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT,'pdf.pdf');
return $response;
}
Twig template:
Download file

CakePHP : override BasicAuthenticate.php

I'm trying to override lib/Cake/Controller/Component/Auth/BasicAuthenticate.php,
because I need to change the unauthenticated() method.
So I copied and modified the file to app/Lib/Cake/Controller/Component/Auth/BasicAuthenticate.php (also tried without the 'Cake' folder), but the changes are not taken into account.
My edit works when modifying directly the core file but I'd rather not.
How should I do ?
I'm using Cake 2.5
Check if you really need to override a core class
To me this looks like you are on the wrong track, overriding the class shouldn't be neccesary unless for example you have no controler over where and how the basic authentication adapter is being used (for example in a plugin that doesn't offer configuration).
If you'd really need to overwrite the class, then the path should be
app/Lib/Controller/Component/Auth/BasicAuthenticate.php
and it should work just fine (it does for me, using CakePHP 2.5.6).
http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#overriding-classes-in-cakephp
Use a custom authentication handler instead
If you have control over the adapter configuration, the I'd suggest that you extend the BasicAuthenticate class instead, and only override the unauthenticate() method, and finally make the auth component use the custom adapter.
Something like
app/Controller/Component/Auth/CustomBasicAuthenticate.php
App::uses('BasicAuthenticate', 'Controller/Component/Auth');
class CustomBasicAuthenticate extends BasicAuthenticate {
public function unauthenticated(CakeRequest $request, CakeResponse $response) {
// do something special
}
}
Controller
public $components = array(
'Auth' => array(
'authenticate' => array(
'CustomBasic'
)
)
);
See also the Creating Custom Authentication objects section in the Cookbook.

CakePHP how to combine process steps

I am developing a system that allows users to download files, but IF they download a file I want to log this action in a special purpose table (MySQL).
I can already generate an icon with a link to appropriate file, but I can't see how I can make the record of the click on the icon to download the file also create the log record.
I am guessing I will have to use a button, and set the action of the button to run ... what? a controller action, a helper function, something else...
It is the last bit that I can't really get my head round. I would appreciate any advice from anyone who may have implemented something similar!
bw
You've got the right idea. Link to a controller action, which will write to the database & log, and will load the file and present it to the user.
Example:
class MyController extends AppController {
// Load the model
public $uses = ('DbTable');
public function get_file() {
// Save the DB record
$this->DbTable->save(...);
// Set the output header for content delivery
// (use the appropriate mime-type for your file)
header('Content-Type: image/jpg');
// Have it download as if it were an attachment
header('Content-Disposition: attachment; filename="filename.jpg"');
// Print out the file contents
echo file_get_contents('/path/to/filename.jpg');
// Prevent any further processing or rendering
exit();
}
}

Extends cakephp plugin

I'm using spark_plug plugin on cakephp, this plugin provides an authentication-acl system for register and admin users in cakephp. I want to add some new code and functionalities to the user's controller but I don't want to change the "main" plugin files.
I was thinking if it is possible leave the "main" plugin controller as it (unchanged) "\app\plugins\spark_plug\controllers\users_controller.php" and create a secondary controller with all the new code and functionalities, something like this "\app\controllers\users_controller.php" and extends the plugin "main" controller.
Is that possible? and how achieve that?
Or do you think is there any other way to do what I want?
Thanks!
You could perhaps use composition rather than inheritance? I.e. create a "app\controllers\users_controller" that has inside it an instance of the plugin's controller. The UsersController passes through any unmodified actions via stubs, eg:
class UsersController extends AppController {
...
var spark_plug_users_controller;
...
public function __construct() {
parent::__construct();
App::import('Controller', 'SparkPlug/Users'); // this is probably wrong.
$this->spark_plug_users_controller = new UsersController; // as is this.
$this->spark_plug_users_controller->constructClasses();
}
...
//example non-overridden method
function login() {
return $this->spark_plug_users_controller->login();
}
...
}
your problem would be accessing protected/private methods within the spark_plug Users controller. But if you did not need to, this may work.

Resources