Gdzie mogę ustawić nagłówki w laravel

Chcę ustawić nagłówki jako array('Cache-Control'=>'no-cache, no-store, max-age=0, must-revalidate','Pragma'=>'no-cache','Expires'=>'Fri, 01 Jan 1990 00:00:00 GMT'); dla wszystkich moich widoków, obecnie robię to we wszystkich kontrolerach zwracając widoki, jak

$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);`

Więc zamiast pisać to dla każdej trasy, można to zrobić w zasięgu globalnym, tak aby nagłówki były ustawione dla każdego widoku.

Próbowałem ustawić nagłówki, tworząc filtr po, ale nie udało się.

Czy ktoś może mi powiedzieć, gdzie mogę ustawić nagłówki dla wszystkich moich widoków?

UPDATE Jeden z moich plików widoku meta content

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

Teraz Kiedy używam Redirect::to('/',301,$headers) Nagłówek w firebug to

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

I kiedy używam Redirect::to('/');

Nagłówek w firebug to

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

7 answers

Istnieje kilka różnych sposobów, aby to zrobić-wszystkie mają zalety/wady.

Wariant 1 (prosty): Ponieważ tablica jest tylko statycznymi danymi - wystarczy ręcznie umieścić nagłówki w układzie widoku bezpośrednio - tzn. nie przekazywać jej z dowolnego miejsca-zakodować ją prosto w widoku.

<?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
?>

Opcja 2: Użyj widok kompozytorów. Możesz użyć aplikacji przed filtrem, aby powiązać nagłówek ze wszystkimi widokami w aplikacji.

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);
}); 
/ Align = "left" / $headers in your view(s).

Uwaga: musisz pozwolić, aby Widok ustawiał Twoje nagłówki - dlatego 'przekazujemy' nagłówek do widoku dla Laravela. Jeśli spróbujesz wypisać nagłówek z filtra lub czegoś takiego, spowodujesz problemy.

Edit Option 3: właśnie się o tym dowiedziałem - możesz spróbować tego

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');
}); 
 31
Author: Laurence,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-09 15:50:11

W Laravel 4 działa mi to:

W filtrach.php:

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

Jak:

App::after(function($request, $response)
{
   $response->headers->set('P3P','CP="NOI ADM DEV PSAi COM NAV OUR OTR STP IND DEM"');
});
 28
Author: Ben Bos,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-08 10:29:40

W Laravel 5, używając Middleware, tworząc nowy plik, modyfikując istniejący plik:

Nowy plik: app / Http/Middleware / AddHeaders.php

<?php namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Routing\Middleware;

// If Laravel >= 5.2 then delete 'use' and 'implements' of deprecated Middleware interface.
class AddHeaders implements Middleware
{
    public function handle($request, Closure $next)
    {
        $response = $next($request);
        $response->header('header name', 'header value');
        $response->header('another header', 'another value');

        return $response;
    }
}

Zmodyfikuj istniejący plik app / Kernel.php

protected $middleware = [
.
.
.

        'App\Http\Middleware\AddHeaders',
    ];
I gotowe.
 28
Author: Amarnasan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-08 22:31:32

Praca nad Laravel 4.2. Używam do tego filtra, więc w filtrach.php mam:

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

});

Następnie dołączam ten filtr do tras lub kontrolerów. Dołączony kontroler wygląda dla mnie tak:

public function __construct() {

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

Ten filtr jest dołączony jako afterFilter.

 5
Author: MikeWu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-07-28 12:43:57

W Laravel 5 możesz zmienić /public / index.PHP linii 55 i ustawić nagłówek dla całej aplikacji:

$response->send();

Z:

$response->header('Content-Type','text/html; charset=ISO-8859-1')->send();
Dla przykładu.
 4
Author: Marius Catalin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-23 10:41:41

For Laravel > = 5.2 yet, following @ Amarnasan answer, although I used mine for API calls

W Laravel 5, używając Middleware, tworząc nowy plik, modyfikując istniejący plik:

Nowy plik: app / Http/Middleware / AddHeaders.php

<?php 
namespace App\Http\Middleware;
use Closure;
use Illuminate\Routing\Redirector;
use Illuminate\Http\Request;
use Illuminate\Foundation\Applicaion;


class AddHeaders 
{
    public function handle($request, Closure $next)
    {
        $response = $next($request);
        $response->header('Cache-Control', 'max-age=36000, public');
        //$response->header('another header', 'another value');

        return $response;
    }
}

Zmodyfikuj istniejący plik app / Kernel.php, dzięki czemu można używać z każdą określoną trasą

protected $routeMiddleware = [
.
.
.

        'myHeader' => \App\Http\Middleware\AddHeaders::class,
    ];

And you're set.

Następnie można go używać w ten sposób dla poszczególnych tras lub grup

$api->get('cars/all', 'MyController@index')->middleware(['myHeader']);;
 4
Author: BlackPearl,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-10 05:07:46

Dla przyszłych czytelników korzystających z Laravel 5.x, to może być obsługiwane po wyjęciu z pudełka bez potrzeby tworzenia niestandardowego oprogramowania pośredniczącego .

Laravel posiada metodę pomocniczą response(), do której można łatwo łączyć nagłówki.

use Response;
// Or possibly: use Illuminate\Http\Response; depending on your aliases used.

// Add a series of headers
return response($content)
    ->header('Content-Type', 'text/xml')
    ->header('X-Header-One', 'Header Value');

// Or use withHeaders to pass array of headers to be added
return response($content)
    ->withHeaders([
        'Content-Type' => 'text/xml',
        'X-Header-One' => 'Header Value'
    ]);

Czytaj więcej na ten temat w dokumentacji , ponieważ może obsługiwać wiele rzeczy; cookies, views, i nie tylko.

 1
Author: camelCase,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-04 19:19:32