繁体   English   中英

在RouteCollection.php第219行中的Laravel 5.2 MethodNotAllowedHttpException

[英]Laravel 5.2 MethodNotAllowedHttpException in RouteCollection.php line 219

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

'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>

错误捕捉: 在此输入图像描述

尝试不使用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

在这里,您将自动为您创建所需的相关功能,以便于使用功能,很酷......

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

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

还要确保创建模型。 例如

运行命令

php artisan make:model Project

在项目模型中 - >

protected $fillable = [
 'name'
];

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

希望这能帮助你。 随意问的问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM