【问题标题】:Method [header] does not exist on view - Laravel 5视图中不存在方法 [标题] - Laravel 5
【发布时间】:2016-04-12 18:56:00
【问题描述】:

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

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>

【问题讨论】:

    标签: php model-view-controller laravel-5


    【解决方案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;
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      您可以通过将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()。 (如果您也发布您的视图代码可能会有所帮助)。

      【讨论】:

      • 发布您的视图的代码,因为错误可能来自那里。具体来说,控制器如何呈现视图。
      • 我已经更新了布局文件。视图是布局的body部分下的普通html文件。
      • 谢谢。您是否还可以包括如何在控制器中调用视图,例如return view('home');
      • 返回视图('home');正如你所料
      猜你喜欢
      • 2018-09-28
      • 1970-01-01
      • 2017-01-29
      • 2015-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-05
      • 1970-01-01
      相关资源
      最近更新 更多