Laravel: POST -> Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49519097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel: POST -> Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
提问by zilijonas
fellow coders.
编码员。
Whenever I try to POST something, I get this error.
每当我尝试发布某些内容时,都会收到此错误。
create.blade.php file:
创建.blade.php 文件:
<h1>Publish a Post</h1>
<hr>
<form method="POST" action="/posts">
{{ csrf_field() }}
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" id="title" name="title">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea type="text" class="form-control" id="body" name="body"></textarea>
</div>
<button type="submit" class="btn btn-primary">Publish</button>
</form>
</div>
web.php file:
web.php 文件:
Route::get('/', 'PostController@index');
Route::get('/posts/create', 'PostController@create');
Route::get('/posts', 'PostController@store');
PostController.php file:
PostController.php 文件:
public function create()
{
return view('posts.create');
}
public function store()
{
dd(request()->all());
}
And the database schema:
和数据库架构:
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('body');
$table->timestamps();
});
}
Any ideas what am I doing wrong or how to fix this issue would be really appreciated.
任何想法我做错了什么或如何解决这个问题将不胜感激。
回答by Chin Leung
You have no POST
defined in your routes. Update your ::get
to ::post
for your endpoint.
您POST
的路线中没有定义。更新您::get
要::post
为您的端点。
Route::post('/posts', 'PostController@store');
For more information: https://laravel.com/docs/5.6/routing#basic-routing
回答by Boghani Chirag
you may register a resourceful route to the controller:
您可以向控制器注册一条资源丰富的路由:
Route::resource('/posts', 'PostController');
Route::resource('/posts', 'PostController');
For more information: https://laravel.com/docs/5.6/controllers