Laravel 5:无法通过 POST 来路由资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31598921/
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 5: Can't POST to route resource
提问by Ronnie
I have a route resource Route::resource('projects', 'ProjectsController');
and when I run route:list
I can see POST is available.
我有一个路由资源Route::resource('projects', 'ProjectsController');
,当我运行时,route:list
我可以看到 POST 可用。
+--------+----------+--------------------------+------------------+--------------------------------------------------------------+-----------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+--------------------------+------------------+--------------------------------------------------------------+-----------------+
| | GET|HEAD | projects | projects.index | App\Http\Controllers\ProjectsController@index | auth |
| | POST | projects | projects.store | App\Http\Controllers\ProjectsController@store | auth |
| | GET|HEAD | projects/create | projects.create | App\Http\Controllers\ProjectsController@create | auth |
| | GET|HEAD | projects/{projects} | projects.show | App\Http\Controllers\ProjectsController@show | auth |
| | PUT | projects/{projects} | projects.update | App\Http\Controllers\ProjectsController@update | auth |
| | PATCH | projects/{projects} | | App\Http\Controllers\ProjectsController@update | auth |
| | DELETE | projects/{projects} | projects.destroy | App\Http\Controllers\ProjectsController@destroy | auth |
| | GET|HEAD | projects/{projects}/edit | projects.edit | App\Http\Controllers\ProjectsController@edit | auth |
+--------+----------+--------------------------+------------------+--------------------------------------------------------------+-----------------+
When I am at /projects/create
(shows my form) and hit my submit button, I get an error saying:
当我在/projects/create
(显示我的表单)并点击我的提交按钮时,我收到一条错误消息:
MethodNotAllowedHttpException in RouteCollection.php line 201:
at RouteCollection->methodNotAllowed(array('GET', 'HEAD', 'PUT', 'PATCH', 'DELETE')) in RouteCollection.php line 188
Is it perhaps how I am defining my <form>
tag? Am I not using the correct action?
也许我是如何定义我的<form>
标签的?我没有使用正确的操作吗?
<form method="post" action="">
<form method="post" action="">
I also tried <form method="post" action="{{ url('projects/store') }}">
我也试过 <form method="post" action="{{ url('projects/store') }}">
Sorry, I am a noob to laravel!
对不起,我是 Laravel 的菜鸟!
回答by Zerp
You should be POST
ing to the resource url, not resource/create.
您应该POST
访问资源 url,而不是资源/创建。
In other words just make sure the action of your form is action="/projects"
not action="/projects/create"
换句话说,只需确保您的表单的操作action="/projects"
不是action="/projects/create"
Edit: I will leave this here as it is kind of relevant, and because I originally posted it, but with the forewarning that it is overkill and a lot of irrelevant code for someone just starting.
编辑:我将把它留在这里,因为它有点相关,因为我最初发布了它,但预先警告它是矫枉过正的,而且对于刚开始的人来说有很多不相关的代码。
For instance, here's a blade snippet from one of my sites:
例如,这是我的一个网站的刀片片段:
@extends('layouts.master')
@section('title', 'Create a Project')
@section('content')
<h3>Create a Project</h3>
<hr/>
{!! Form::open(['action'=>'ProjectController@store']) !!}
@include('forms/partials/edit_form', ['submit_button_label' => 'Add Project'])
{!! Form::close() !!}
@include('errors.list')
@endsection
回答by Kyle
Laravel actually uses method="POST"
in all <form>
tags.
Laravel 实际上用于method="POST"
所有<form>
标签。
What you need is the following:
您需要的是以下内容:
<input type="hidden" name="_method" value="DELETE">
DELETE
can be replaced with the other HTTP verbs too (PUT, PATCH, UPDATE, etc)
DELETE
也可以替换为其他 HTTP 动词(PUT、PATCH、UPDATE 等)
回答by daneczech
I see that since the last reply Laravel has been updated. Anyway, I came across the same problem today and here is how I fixed it.
我看到自从上次回复 Laravel 已经更新。无论如何,我今天遇到了同样的问题,这是我修复它的方法。
Basically my routing looks like this now:
基本上我的路由现在看起来像这样:
// Resourcing routes: https://laravel.com/docs/5.3/controllers#resource-controllers
Route::resource('admin/photos', 'Admin\AdminPhotosController');
// need to do this to enable the store method on the following route (otherwise POST is on index when resourcing controllers)
Route::any('admin/photos/create', 'Admin\AdminPhotosController@create');
Route::post('admin/photos/create', 'Admin\AdminPhotosController@store');
Hope that helps someone.
希望能帮助某人。