【问题标题】:Laravel 5.2 MethodNotAllowedHttpException in RouteCollection.php line 219RouteCollection.php 第 219 行中的 Laravel 5.2 MethodNotAllowedHttpException
【发布时间】:2016-06-15 01:58:57
【问题描述】:

首先我的错误是找不到类输入所以我添加了

'Input' => Illuminate\Support\Facades\Input::class,

在别名数组中

现在当我提交我的表单时,它给出了这个错误

错误:RouteCollection.php 第 219 行中的 MethodNotAllowedHttpException:

Routes.php

Route::post('add', function () {
    $name = Input::get('name');
    if(DB::table('projects')->whereName($name)->first() != NULL) return 'already exist';
    DB::table('projects')->insert(array('name'=>'$name'));
    return Redirect::to('/add'); 
});

welcome.blade.php:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Laravel Learning</title>
</head>
    <body>
            {!! Form::open(array('url' => 'add')) !!}
                {!! Form::text('name', 'Your Name...') !!}
                {!! Form::submit('Click Me!') !!}
            {!! Form::close() !!}   
    </body>
</html>

错误快照:

【问题讨论】:

  • 数据是否被插入?由于您还重定向到return Redirect::to('/add');,这将是一个GET url。
  • 同时传递 csrf_token
  • @Ciccio nopx 我只看到了那个错误,没有任何东西插入到数据库中!
  • @aldrin27 是必须的吗?
  • 来自文档If you use the Form::open method with POST, PUT or DELETE the CSRF token will be added to your forms as a hidden field automatically.,所以如果您检查页面源(在浏览器中),我怀疑它已经存在了。

标签: php forms laravel-5.2


【解决方案1】:

尝试不使用routes.php直接执行函数。意思是,路由中使用的功能应该在控制器中,也许这就是 laravel 5.1 不允许您执行任务的原因。

让您更好地了解工作流程

routes.php ->

Route::resource('projects', 'projectController');

welcome.blade.php ->

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Laravel Learning</title>
</head>
<body>
        {!! Form::open(array('url' => 'projects')) !!}
            {!! Form::text('name', 'Your Name...') !!}
            {!! Form::submit('Click Me!') !!}
        {!! Form::close() !!}   
</body>
</html>

然后,转到您的 cmd,浏览到您的项目文件夹,然后启动命令

php artisan make:controller projectController

这里会自动为你创建你需要的相关功能,方便使用功能,酷呵呵...

现在将您的添加逻辑写入创建函数。

public function store()
{
  Project::create(Request::all());
  //here you can write your return redirect(''); 
}

还要确保您创建了一个模型。例如

运行命令

php artisan make:model Project

在项目模型中 ->

protected $fillable = [
 'name'
];

使用此可填充数组的目的是出于安全目的以避免批量分配漏洞。

希望这对您有所帮助。欢迎提问

【讨论】:

    猜你喜欢
    • 2016-06-20
    • 2016-07-30
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 1970-01-01
    相关资源
    最近更新 更多