Laravel - 访问被禁止!您无权访问所请求的对象。它受读保护或服务器无法读取

时间:2016-02-03 14:32:35

标签: php laravel

PHP

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Laravel PHP Framework</title>

</head>
<body>
  <div class="welcome">
      <h1>test page</h1>

      <form action="{{ URL::route('dis') }}" method="post">
        First name:<br>
        <input type="text" name="firstname"><br>
        Last name:<br>
        <input type="text" name="lastname">
        <input type="hidden" name="_token" value="{{ csrf_token() }}">

        <input type="submit">   
      </form>

  </div>
</body>
</html> 

display.blade.php

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>display page</title>

</head>
<body>
    <div class="welcome">
        <h1>test page for display</h1>

    </div>
</body>
</html>

routes.php文件

<?php
 Route::get('/', array('uses'=>'HomeController@showWelcome', 'as' => 'home'));

 Route::group(array('before'=>'csrf'),function()
 {
     Route::post('/dis', array('uses'=>'HomeController@display', 'as' => 'dis'));
 });

HomeController.php

<?php

class HomeController extends BaseController {


    public function showWelcome()
    {
        return View::make('hello');
    }

    public function display()
    {
        return View::make('display');
    }

 }

laravel 5中有错误, 当我尝试提交表格hello.html时出现以下错误。

禁止访问! 您无权访问所请求的对象。它受读保护或服务器无法读取。

1 个答案:

答案 0 :(得分:0)

我看到的一件事是您需要表单中的CSRF令牌。如果您还安装了illuminate\html(现在Laravel 5中未包含),则可以使用Form::open而不是在HTML中创建<form>标记来获取它。

或者,您只需调用csrf_field函数:

即可将令牌添加到表单中
{!! csrf_field() !!}