2014-12-10 3 views
5

Используя laravel, я пытаюсь добавить свои собственные заголовки ко всем ответам сервера.Laravel Not добавления пользовательских заголовков

У меня есть следующие в filters.php:

App::after(function($request, $response) 
{ 
    // security related 
    $response->headers->set('X-Frame-Options','deny'); // Anti clickjacking 
    $response->headers->set('X-XSS-Protection', '1; mode=block'); // Anti cross site scripting (XSS) 
    $response->headers->set('X-Content-Type-Options', 'nosniff'); // Reduce exposure to drive-by dl attacks 
    $response->headers->set('Content-Security-Policy', 'default-src \'self\''); // Reduce risk of XSS, clickjacking, and other stuff 
    // Don't cache stuff (we'll be updating the page frequently) 
    $response->headers->set('Cache-Control', 'nocache, no-store, max-age=0, must-revalidate'); 
    $response->headers->set('Pragma', 'no-cache'); 
    $response->headers->set('Expires', 'Fri, 01 Jan 1990 00:00:00 GMT'); 
    // CRITICAL: do NOT delete 
    $response->headers->set('X-Archer', 'DANGER ZONE'); 
}); 

Еще нет новых заголовков не появляется, когда я проверить:

[tesla | ~] => curl -o/dev/null -s -D - localhost 
HTTP/1.1 200 OK 
Date: Wed, 10 Dec 2014 23:13:30 GMT 
Server: Apache 
X-Powered-By: PHP/5.6.2 
Content-Length: 974 
Content-Type: text/html; charset=UTF-8 

[tesla | ~] => 

У меня нет ошибки или предупреждения в моих журнальных файлах. Как это могло произойти?

+0

Какая версия Laravel? В 4 это 'header()' вместо 'headers-> set()' http://laravel.com/docs/4.2/responses – mopo922

+0

@ mopo922 Я использую 4.2, но меняю его на заголовок '$ response-> ('key', 'val') 'ничего не менял – 735Tesla

+0

Другая вещь, которую вы могли попробовать, - это просто обычный PHP' header() ' – mopo922

ответ

4

Попробуйте это: В функции контроллера, который вызывает вид, следовать с вызовом к классу «Response»:

$contents = View::make('your_view')->with('data', $data); 
$response = Response::make($contents, 200); 
$response->header('X-Frame-Options','deny'); // Anti clickjacking 
$response->header('X-XSS-Protection', '1; mode=block'); // Anti cross site scripting (XSS) 
$response->header('X-Content-Type-Options', 'nosniff'); // Reduce exposure to drive-by dl attacks 
$response->header('Content-Security-Policy', 'default-src \'self\''); // Reduce risk of XSS, clickjacking, and other stuff 
    // Don't cache stuff (we'll be updating the page frequently) 
$response->header('Cache-Control', 'nocache, no-store, max-age=0, must-revalidate'); 
$response->header('Pragma', 'no-cache'); 
$response->header('Expires', 'Fri, 01 Jan 1990 00:00:00 GMT'); 
return $response; 

Конечно, вы могли бы реорганизовать выше, и включить его в вспомогательную функцию.

+0

Это было! Благодаря! – 735Tesla

+0

'$ response-> header ('X-Archer', 'DANGER ZONE');' почему? –

+0

@ LukasBernhard - вы можете игнорировать этот настраиваемый заголовок. Удалены. – FredTheWebGuy

Смежные вопросы