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

时间:2016-04-01 11:18:03

标签: php model-view-controller 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;
    }
}

我在app / http / 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第387行中的BadMethodCallException:

     
    

视图中不存在方法[标题]。

  

请帮助我!

更新

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

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

2 个答案:

答案 0 :(得分:1)

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

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

}

答案 1 :(得分:0)

您可以将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。从错误消息中,您似乎还在header使用view()。 (如果您还发布了您的视图代码,可能会有帮助。)