在CakePHP 2中的Controller的beforeFilter中发送标头

I have myController which is extended from AppController.
Inside myController::beforeFilter I put this line:

header('HTTP/1.0 401 Unauthorized', true, 401);

But I can't see this data inside response headers.
I don't have any whitespace inside this class neither in AppController.
Where should I look or how can I debug this issue?
Thank you

Instead of using PHP's header function, you'll want to use Cake's:

In AppController::beforeFilter:

$this->header('HTTP/1.0 401 Unauthorized');

Using Cake's built in header function queues the headers and sends them all at once when it's ready. I think your problem is likely because you're outputting a header at the wrong time.