我在哪里可以在laravel中设置标题

时间:2013-07-09 12:48:09

标签: laravel laravel-4

我想为所有视图设置标题为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);`

因此,不是为每个路径写这个都可以在全局范围内完成,所以为每个视图设置了标题。

我尝试通过在过滤后创建来设置标题,但是没有让它工作。

有谁能告诉我在哪里可以为我的所有观看设置标题?

更新 我的一个视图文件元内容

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

现在当我使用Redirect::to('/',301,$headers)时 萤火虫的标题是

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('/');

萤火虫的标题是

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

7 个答案:

答案 0 :(得分:32)

有几种不同的方法可以做到这一点 - 都有优点/缺点。

选项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:使用view composers。您可以在过滤前使用应用程序将标题绑定到应用中的所有视图。

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);
}); 

然后在你的视图中回显$ header。

注意:您必须让视图设置标题 - 这就是我们将标题“传递”到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');
}); 

答案 1 :(得分:31)

在Laravel 5中,使用中间件,创建新文件,修改现有文件:

新文件: 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;
    }
}

修改现有文件 app / Kernel.php

protected $middleware = [
.
.
.

        'App\Http\Middleware\AddHeaders',
    ];

你已经确定了。

答案 2 :(得分:28)

在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"');
});

答案 3 :(得分:8)

在Laravel 5中,您可以更改/public/index.php第55行并为整个应用设置标题:

$response->send();

使用:

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

代表。

答案 4 :(得分: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。

答案 5 :(得分:4)

对于Laravel&gt; = 5.2,在@Amarnasan回答之后,虽然我使用了我的API调用

在Laravel 5中,使用中间件,创建新文件,修改现有文件:

新文件: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;
    }
}

修改现有文件app / Kernel.php,以便您可以使用每条特定路线

protected $routeMiddleware = [
.
.
.

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

And you're set.

然后你就可以像个人路线或团体一样使用它

$api->get('cars/all', 'MyController@index')->middleware(['myHeader']);;

答案 6 :(得分:1)

对于使用Laravel 5.x的未来读者,可以开箱即用处理,无需创建任何自定义中间件

Laravel使用response()辅助方法,您可以非常轻松地链接标题。

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'
    ]);

documentation中详细了解它,因为它可以处理许多事情; cookiesviews等。