方法[header]在视图上不存在-Laravel 5


Method [header] does not exist on view - Laravel 5

我创建了一个中间件来限制页面返回仪表板,如下所示:

namespace App'Http'Middleware;
use Closure;
class ValidateBackButton {
/**
 * Handle an incoming request.
 *
 * @param  'Illuminate'Http'Request  $request
 * @param  'Closure  $next
 * @return mixed
 */
public function handle($request, Closure $next)            
    {            
            $response = $next($request);
            $response->header('Cache-Control','nocache, no-store, max-age=0, must-revalidate')
            ->header('Pragma','no-cache') 
            ->header('Expires','Sat, 01 Jan 1990 00:00:00 GMT');
            return $response;
    }
}

我已经在apphttp://kernel.php 中注册了中间件

protected $routeMiddleware = [
    'auth' => 'App'Http'Middleware'Authenticate',
    'auth.basic' => 'Illuminate'Auth'Middleware'AuthenticateWithBasicAuth',
    'guest' => 'App'Http'Middleware'RedirectIfAuthenticated',
    'validateback' => 'App'Http'Middleware'ValidateBackButton',
];

我在我的控制器中使用中间件:

public function __construct()
{
    $this->middleware('auth');
    $this->middleware('validateback');
}

一切似乎都很好,然而,我收到以下错误消息:

View.php中的BadMethodCallException第387行:

视图中不存在方法[header]。

请帮帮我!

更新

我认为视图应该没有任何问题,所以我有下面的布局文件。视图的代码太长,无法将其放在此处。

<!DOCTYPE html>
<!--[if IE 8]> <html lang="en" class="ie8 no-js"> <![endif]-->
<!--[if IE 9]> <html lang="en" class="ie9 no-js"> <![endif]-->
<!--[if !IE]><!-->
<html lang="en" class="no-js">
<!--<![endif]-->
<head>
<meta charset="utf-8"/>
<title>SB Admin v2.0 in Laravel 5</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta content="width=device-width, initial-scale=1" name="viewport"/>
<meta content="" name="description"/>
<meta content="" name="author"/>
<link rel="stylesheet" href="{{ asset("assets/stylesheets/styles.css") }}" />
</head>
<body>
@yield('body')
<script src="{{ asset("assets/scripts/frontend.js") }}" type="text/javascript"></script>
</body>
</html>

以下更改解决了我的问题:

namespace App'Http'Middleware;
use Closure;
use Illuminate'Http'RedirectResponse;
class ValidateBackButton { 
/**
 * Handle an incoming request.
 *
 * @param  'Illuminate'Http'Request  $request
 * @param  'Closure  $next
 * @return mixed
 */
public function handle($request, Closure $next)            
    {            
            //$response = $next($request);
            $response = $next($request);
            $response = $response instanceof RedirectResponse ? $response : response($response);
            return $response->header('Cache-Control','nocache, no-store, max-age=0, must-revalidate')
                            ->header('Pragma','no-cache') //HTTP 1.0
                            ->header('Expires','Sat, 01 Jan 1990 00:00:00 GMT'); // // Date in the past
            return $response;
    }
}

您可以通过将header链接到$next:来修复此问题

return $next($request)->header('Cache-Control','nocache, no-store, max-age=0, must-revalidate')
            ->header('Pragma','no-cache') 
            ->header('Expires','Sat, 01 Jan 1990 00:00:00 GMT');

如果这没有帮助,请确保您没有在其他地方使用header。从错误消息中,您似乎也在使用headerview()。(如果你也发布你的视图代码,可能会有所帮助)。