Return custom file response

0 views
Skip to first unread message

lsolesen

unread,
Mar 6, 2010, 1:03:47 PM3/6/10
to Konstrukt
In a legacy application I used to do this:

function file() {
header('Content-Type: '.$this->mime_type);
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $this-
>last_modified).' GMT');
header('Cache-Control:');
header('Content-Disposition: inline; filename='.$this-
>file_name);
header('Pragma:');
return $this->fetchContent();
}

echo $file();

How can I return this using a k_*Response*?

class FileViewerComponent extends k_Component
{
protected $mime_type;
protected $last_modified;
protected $file_name;

function GET()
{
// how do one construct the response
return k_*Response*();
}
}

I am using GET() instead of renderHtml() as it must be more correct?
Do you agree?

troels knak-nielsen

unread,
Mar 6, 2010, 5:08:00 PM3/6/10
to kons...@googlegroups.com
On Sat, Mar 6, 2010 at 7:03 PM, lsolesen <lsol...@gmail.com> wrote:
> I am using GET() instead of renderHtml() as it must be more correct?
> Do you agree?

Yes, certainly. You're not returning a html document for this component.

On Sat, Mar 6, 2010 at 7:03 PM, lsolesen <lsol...@gmail.com> wrote:
> How can I return this using a k_*Response*?

You can do:

class FileViewerComponent extends k_Component {
protected $mime_type;
protected $last_modified;
protected $file_name;
function GET() {

$response = new k_HttpResponse(200, $this->fetchContent());
$response->setHeader('Content-Type', $this->mime_type);
$response->setHeader('Last-Modified', gmdate('D, d M Y H:i:s',
$this->last_modified).' GMT');
$response->setHeader('Cache-Control', 'private');
$response->setHeader('Content-Disposition', 'inline;
filename='.$this->file_name);
$response->setHeader('Pragma', 'cache');
$response->setContent();
return $response;
}
}

--
troels

Reply all
Reply to author
Forward
0 new messages