Php 视图-Laravel 5上不存在方法[header]
我创建了一个中间件来限制页面返回仪表板,如下所示:Php 视图-Laravel 5上不存在方法[header],php,model-view-controller,laravel-5,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 ha
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:
视图中不存在方法[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>
SB Admin v2.0在Laravel 5中的应用
@屈服(‘体’)
您可以通过将标题链接到$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');
如果这没有帮助,请确保您没有在其他地方使用标题。从错误消息中,您似乎也在使用标题和视图()
。(如果您也发布视图代码,可能会有所帮助)。以下更改解决了我的问题:
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;
}
}
为您的视图发布代码,因为可能会出现错误。具体来说,控制器呈现视图的方式。我已使用布局文件进行了更新。视图是版面主体部分下的普通html文件。谢谢。还可以包括如何在控制器中调用视图,例如返回视图(“主视图”)代码>返回视图(“主视图”);如你所料