Header Issue?

6 views
Skip to first unread message

Chris Boden

unread,
Mar 27, 2011, 11:58:27 PM3/27/11
to Alloy
Inside a view I tried to set the header, but it didn't seem to take:

<?php
$h = (int)headers_sent();
$k = Kernel();
$k->response()->contentType('text/plain');

echo 'Headers?: ' . $h;
?>

Header Result:
Content-Type:text/html

Content Result:
Headers?: 0

Vance Lucas

unread,
Mar 28, 2011, 10:08:20 AM3/28/11
to allo...@googlegroups.com
This is because headers are not sent immediately - they are just set on the Response object. The headers are sent in www/index.php after the module response has been processed, but not yet rendered.

Look at lines 161-162:
https://github.com/alloyphp/alloy/blob/master/www/index.php#L162

If you need to send headers manaully, just make a call to the "sendHeaders" method of the Response object.

Chris Boden

unread,
Mar 28, 2011, 10:13:51 AM3/28/11
to Alloy
Hi Vance,

The header_sent() was just a check to make sure I wasn't setting the
header too late.

The issue is Alloy seems to be over writing my contentType setting of
the header.

I set it to text/plain but Alloy output text/html (default).


On Mar 28, 10:08 am, Vance Lucas <va...@vancelucas.com> wrote:
> This is because headers are not sent immediately - they are just set on the
> Response object. The headers are sent in www/index.php after the module
> response has been processed, but not yet rendered.
>
> Look at lines 161-162:https://github.com/alloyphp/alloy/blob/master/www/index.php#L162
>
> If you need to send headers manaully, just make a call to the "sendHeaders"
> method of the Response object.
>
> --
> Vance Lucashttp://vancelucas.com

Vance Lucas

unread,
Mar 28, 2011, 10:18:05 AM3/28/11
to allo...@googlegroups.com
Update the code block to this and see what happens:

<?php
$h = (int)headers_sent();
$k = Kernel();
$k->response()->contentType('text/plain');
$k->response()->sendHeaders();

echo 'Headers?: ' . $h;
?>

I'll look into other places that might be overriding this.

Chris Boden

unread,
Mar 28, 2011, 10:26:13 AM3/28/11
to Alloy
That worked, thanks Vance.

I don't think you need to look further into it...I think I figured out
my error. I tried to set the header in the view...while PHP had not
sent the headers yet Alloy had already done its part with buffering
and setting the headers. I was trying to set the headers through
Alloy after it had rendered its part but before PHP.

On Mar 28, 10:18 am, Vance Lucas <va...@vancelucas.com> wrote:
> Update the code block to this and see what happens:
>
> <?php
> $h = (int)headers_sent();
> $k = Kernel();
> $k->response()->contentType('text/plain');
> $k->response()->sendHeaders();
>
> echo 'Headers?: ' . $h;
> ?>
>
> I'll look into other places that might be overriding this.
>
> --
> Vance Lucashttp://vancelucas.com
Reply all
Reply to author
Forward
0 new messages