我有一个路由资源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 |
+--------+----------+--------------------------+------------------+--------------------------------------------------------------+-----------------+
当我在/projects/create
(显示我的表单)并点击我的提交按钮时,我收到错误消息:
MethodNotAllowedHttpException in RouteCollection.php line 201:
at RouteCollection->methodNotAllowed(array('GET', 'HEAD', 'PUT', 'PATCH', 'DELETE')) in RouteCollection.php line 188
我可能是如何定义<form>
标签的吗?我没有使用正确的行动吗?
<form method="post" action="">
我也试过<form method="post" action="{{ url('projects/store') }}">
对不起,我是laravel的菜鸟!
答案 0 :(得分:7)
您应该POST
到资源网址,而不是资源/创建。
换句话说,只需确保您的表单操作为action="/projects"
而不是action="/projects/create"
编辑: 我会把它留在这里,因为它有点相关,而且因为我最初发布它,但是预先警告它对于刚刚开始的人来说是过度杀伤和许多不相关的代码。
例如,这是我网站上的一个刀片片段:
@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
答案 1 :(得分:3)
Laravel在所有method="POST"
代码中实际使用<form>
。
您需要的是以下内容:
<input type="hidden" name="_method" value="DELETE">
DELETE
也可以替换为其他HTTP谓词(PUT,PATCH,UPDATE等)
答案 2 :(得分:0)
我看到自上次回复以来Laravel已经更新。无论如何,我今天遇到了同样的问题,这就是我如何修复它。
基本上我的路由现在看起来像这样:
// 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');
希望能有所帮助。