本文介绍了Laravel 5:无法发布以路由资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个路由资源Route::resource('projects', 'ProjectsController');,当我运行route:list时,我可以看到POST可用.

I have a route resource Route::resource('projects', 'ProjectsController'); and when I run route:list I can see POST is available.

+--------+----------+--------------------------+------------------+--------------------------------------------------------------+-----------------+
| 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处(显示我的表单)并点击我的提交按钮时,我收到一条错误消息:

When I am at /projects/create (shows my form) and hit my submit button, I get an error saying:

MethodNotAllowedHttpException in RouteCollection.php line 201:
  at RouteCollection->methodNotAllowed(array('GET', 'HEAD', 'PUT', 'PATCH', 'DELETE')) in RouteCollection.php line 188

也许是我如何定义<form>标签?我没有采取正确的措施吗?

Is it perhaps how I am defining my <form> tag? Am I not using the correct action?

<form method="post" action="">

我也尝试过<form method="post" action="{{ url('projects/store') }}">

对不起,我是laravel的菜鸟!

Sorry, I am a noob to laravel!

推荐答案

您应该POST进入资源url,而不是resource/create.

You should be POSTing to the resource url, not resource/create.

换句话说,只需确保表单的操作是action="/projects"而不是action="/projects/create"

In other words just make sure the action of your form is action="/projects" not action="/projects/create"

我将把它留在这里,因为它是相关的,并且因为我最初发布了它,但是警告说它太过分了,并且对于刚入门的人来说有很多无关的代码.

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

这篇关于Laravel 5:无法发布以路由资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 18:04