Laravel 5.4

时间:2017-06-19 05:04:49

标签: php laravel laravel-5 laravel-5.4

我是Laravel和PHP的新手。我正在使用POST方法来获取用户请求,并且我在verifyCSRFToken.php第68行中获取了TokenMismatchException。检查了server.php中的日志,发现以下内容。

[2017-06-13 11:29:55] local.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Undefined constant 'home' in C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Container\Container.php:762

堆栈追踪:

C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Container\Container.php(762): ReflectionClass->newInstanceArgs(Array)
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Container\Container.php(608): Illuminate\Container\Container->build('App\\Http\\Middle...')
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Container\Container.php(575): Illuminate\Container\Container->resolve('App\\Http\\Middle...')
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(72Illuminate\Container\Container->make('App\\Http\\Middle...')
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(138): Illuminate\Foundation\Application->make('App\\Http\\Middle...')
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\View\Middleware\ShareErrorsFromSession.php(49): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Session\Middleware\StartSession.php(64): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse.php(37): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\EncryptCookies.php(59): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Routing\Router.php(576): Illuminate\Pipeline\Pipeline->then(Object(Closure))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Routing\Router.php(535): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Routing\Router.php(513): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(174): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(30): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(30): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(30): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ValidatePostSize.php(27): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Illuminate\Http\Request), Object(Closure))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php(46): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(14 Illuminate\Pipeline\Pipeline->then(Object(Closure))
C:\Users\Dikesh Kumar\blog\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(11 Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
C:\Users\Dikesh Kumar\blog\public\index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
C:\Users\Dikesh Kumar\blog\server.php(21): require_once('C:\\Users\\Dikesh...')
{main}  

有人可以帮忙吗?

5 个答案:

答案 0 :(得分:6)

Laravel可以轻松保护您的应用程序免受跨站点请求伪造(CSRF)攻击。跨站点请求伪造是一种恶意攻击,即代表经过身份验证的用户执行未经授权的命令。

Laravel自动生成CSRF"令牌"对于应用程序管理的每个活动用户会话。此令牌用于验证经过身份验证的用户是否是实际向应用程序发出请求的用户。

无论何时在应用程序中定义HTML表单,都应在表单中包含隐藏的CSRF令牌字段,以便CSRF保护中间件可以验证请求。您可以使用csrf_field帮助程序生成令牌字段:

<form method="POST" action="/profile">
    {{ csrf_field() }}
    ...
</form>

Reference

答案 1 :(得分:1)

您需要在元标记中定义csrf令牌:

<meta name="csrf-token" content="{{ csrf_token() }}">

或者您也可以传递隐藏的输入元素:

<input type="hidden" name="_token" value="{{ csrf_token() }}">

如果您使用ajax,则必须将csrf值定义为:

<script type="text/javascript">
      $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
      });
  </script>

然后运行以下commnad:

php artisan cache:clear

答案 2 :(得分:1)

我认为你所说的是你知道如何使用csrf令牌,但是,在一段时间后你得到令牌不匹配。例如,如果您的登录页面在浏览器中停留了一段时间,表单已经生成了令牌(作为隐藏字段),则会发生这种情况。现在,当您尝试填写表单时,您正在使用上次刷新页面时生成的令牌(例如:昨天)。在后面,一旦您提交表单,laravel将实例化一个新令牌并将与该令牌进行比较,这将是无效的。

您可以将public function index() { $stocks = Stock::all(); $stockInfos = []; foreach ($stocks as $stock) { array_push($stockInfos,DB::table('stocksList') ->where('stockCode', $stock->stockCode) ->take(3) ->get()); } return view('welcome', compact('stocks'))->with('stockInfos',collect($stockInfos)); } 内的会话到期时间从120分钟更改为更长的时间。

否则,您可以每隔120分钟刷新一次HTML元刷新标记,这样当表单处于空闲状态时,您可以刷新页面并打印最新的csrf令牌。

答案 3 :(得分:0)

我遇到了同样的问题,这个错误可能来自多个来源。我没找到答案,所以我在laravel升级中挖掘。我使用数据库驱动程序,所以我查看了会话表迁移。一些新字段可以为空。 我修复了我的表并且它工作。所以如果你有tokenmismatch的问题,检查laravel是否可以创建会话(文件或数据库)。

答案 4 :(得分:0)

app/Http/helpers.php中,您将在文件开头找到一个空格,然后<?php将其删除。