58

すべてのビューについてヘッダーを設定したいのですがarray('Cache-Control'=>'no-cache, no-store, max-age=0, must-revalidate','Pragma'=>'no-cache','Expires'=>'Fri, 01 Jan 1990 00:00:00 GMT');、現在、ビューを返しながらすべてのコントローラーでこれを行っています。

$headers=array('Cache-Control'=>'no-cache, no-store, max-age=0, must-revalidate','Pragma'=>'no-cache','Expires'=>'Fri, 01 Jan 1990 00:00:00 GMT');

Redirect::to('/',301,$headers);`

したがって、ルートごとにこれを記述する代わりに、グローバル スコープで実行できるため、すべてのビューにヘッダーが設定されます。

after フィルターを作成してヘッダーを設定しようとしましたが、うまくいきませんでした。

すべてのビューのヘッダーをどこに設定できますか?

UPDATE ビューファイルのメタコンテンツの1つ

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>

今私が使用するときRedirect::to('/',301,$headers) 、firebugのヘッダーは

Cache-Control   max-age=0, must-revalidate, no-cache, no-store, private
Connection  Keep-Alive
Content-Type    text/html; charset=UTF-8
Date    Tue, 09 Jul 2013 14:52:08 GMT
Expires Fri, 01 Jan 1990 00:00:00 GMT

そして、私が使用するときRedirect::to('/');

firebugのヘッダーは

Cache-Control   no-cache
Connection  Keep-Alive
Content-Type    text/html; charset=UTF-8
Date    Tue, 09 Jul 2013 14:52:08 GMT
4

7 に答える 7

38

これを行うにはいくつかの方法があります - すべてに長所と短所があります。

オプション 1 (シンプル): 配列は単なる静的データであるため、手動でヘッダーをビュー レイアウトに直接配置するだけです。つまり、どこからでも渡さないでください。ビューで直接コーディングします。

<?php
  //set headers to NOT cache a page
  header("Cache-Control: no-cache, must-revalidate"); //HTTP 1.1
  header("Pragma: no-cache"); //HTTP 1.0
  header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>

オプション 2:ビュー コンポーザを使用します。アプリの前にフィルターを使用して、ヘッダーをアプリ内のすべてのビューにバインドできます。

App::before(function($request)  
{
     $headers=array('Cache-Control'=>'no-cache, no-store, max-age=0, must-revalidate','Pragma'=>'no-cache','Expires'=>'Fri, 01 Jan 1990 00:00:00 GMT');

     View::share('headers', $headers);
}); 

次に、ビューで $headers をエコーアウトします。

注: ビューにヘッダーを設定させる必要があります。これが、Laravel が処理できるようにヘッダーをビューに「渡す」理由です。フィルターなどからヘッダー自体を出力しようとすると、問題が発生します。

編集オプション 3:私はちょうどこれについて知りました - あなたはこれを試すことができます

App::before(function($request)  
{
     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');
}); 
于 2013-07-09T14:00:07.290 に答える
29

Laravel 4では、これは私にとってはうまくいきます:

filters.php で:

App::after(function($request, $response)
{
   $response->headers->set('key','value');
});

お気に入り:

App::after(function($request, $response)
{
   $response->headers->set('P3P','CP="NOI ADM DEV PSAi COM NAV OUR OTR STP IND DEM"');
});
于 2013-08-08T09:37:00.560 に答える
11

Laravel 5 では、 /public/index.php 行 55 を変更して、アプリ全体のヘッダーを設定できます。

$response->send();

と:

$response->header('Content-Type','text/html; charset=ISO-8859-1')->send();

エサンプル用。

于 2015-07-23T10:41:41.023 に答える
5

Laravel 4.2 に取り組んでいます。私はこれにフィルターを使用しているので、filters.php には次のものがあります。

Route::filter('no-cache',function($route, $request, $response){

    $response->header("Cache-Control","no-cache,no-store, must-revalidate");
    $response->header("Pragma", "no-cache"); //HTTP 1.0
    $response->header("Expires"," Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

});

このフィルターをルートまたはコントローラーにアタッチするよりも。接続されたコントローラーは、私にとっては次のようになります。

public function __construct() {

        $this->beforeFilter('onestep',array('except' => 'getLogin'));
        $this->beforeFilter('csrf',array('on' => 'post'));
        $this->afterFilter("no-cache", ["only"=>"getIndex"]);
    }

このフィルタは afterFilter として添付されます。

于 2014-07-28T12:43:57.223 に答える