【问题标题】:PUT/POST in LaravelLaravel 中的 PUT/POST
【发布时间】:2020-05-31 04:46:02
【问题描述】:

我是 Laravel 的新手,并且正在学习 Laracasts 的 [Laravel 6 from Scratch][1] 课程。该课程是免费的,但我买不起 Laracasts 会员资格,所以我不能在那里提问。

我已经完成了课程的第 6 部分,控制器技术,并且在尝试扩展我们迄今为止所做的工作以添加一些新功能时遇到了意想不到的问题。该课程让学生构建页面,让用户显示文章列表、查看单个文章、创建和保存新文章,以及更新和保存现有文章。课程工作设想了一篇非常简单的文章,其中只包含一个 ID(在数据库中自动递增,网络用户不可见)、标题、摘录和正文,我得到了所有的功能,包括更新现有文章并保存。

更新表单将方法设置为 POST,但随后使用 @METHOD('PUT') 指令告诉浏览器它实际上应该执行 PUT。这在原始代码中完美运行。但是,既然我在表单中添加了另外两个字段,当我在编辑现有记录后单击提交时,保存失败并显示以下消息:

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The PUT method is not supported for this route. Supported methods: GET, HEAD, POST.
http://localhost:8000/articles 

我不明白为什么在表单中添加两个字段会导致它中断。有人可以启发我吗?我将两个新字段/列添加到迁移中并运行 migrate:rollback 和 migrate。我还向可填充属性添加了新字段/列,并在 ArticlesController 中为它们添加了验证。

这是我的路线:

Route::get('/articles', 'ArticlesController@index');
Route::post('/articles', 'ArticlesController@store');
Route::get('/articles/create', 'ArticlesController@create');
Route::get('/articles/{article}', 'ArticlesController@show');
Route::get('/articles/{article}/edit', 'ArticlesController@edit');
Route::put('/articles/{article}', 'ArticlesController@update');
//Route::delete('/articles/{article}', ArticlesController@destroy');

这是我的 ArticlesController:

<?php

namespace App\Http\Controllers;

use App\Article;
use Illuminate\Http\Request;

class ArticlesController extends Controller
{
public function index()
{
    $articles = Article::latest()->get();
    return view ('articles.index', ['articles' => $articles]);
}

public function show(Article $article) 
{
    return view('articles.show', ['article' => $article]);
}

public function create() 
{
    return view('articles.create');
}

public function store() 
{
    //Stores a NEW article
    Article::create($this->validateArticle());
    return redirect('/articles');
}   

public function edit(Article $article) 
{
    return view('articles.edit', ['article' => $article]);
}

public function update(Article $article)
{
    //Updates an EXISTING article
    $article->update($this->validateArticle());
    return redirect('/articles/', $article->id);
}

public function validateArticle()
{
    return request()->validate([
        'title' => ['required', 'min:5', 'max:20'],
        'author' => ['required', 'min:5', 'max:30'],
        'photopath' => ['required', 'min:10', 'max:100'],
        'excerpt' => ['required', 'min:10', 'max:50'],
        'body' => ['required', 'min:50', 'max:500']
    ]);
}

public function destroy(Article $article)
{
    //Display existing record with "Are you sure you want to delete this? Delete|Cancel" option
    //If user chooses Delete, delete the record
    //If user chooses Cancel, return to the list of articles
}
}

这是我的编辑表单,edit.blade.php:

@extends('layout')

@section('content')
<div id="wrapper">
<div id="page" class="container">
    <h1>Update Article</h1>
    <form method="POST" action="/articles">
        @csrf
        @method('PUT')
        <div class="form-group">
            <label class="label" for="title">Title</label>
            <div class="control">
                <input class="form-control @error('title') errorborder @enderror" type="text" name="title" id="title" value="{{ $article->title }}">
                @error('title')
                    <p class="errortext">{{ $errors->first('title') }}</p> 
                @enderror
            </div>
        </div>
        <div class="form-group">
            <label class="label" for="author">Author</label>
            <div class="control">
                <input class="form-control @error('author') errorborder @enderror" type="text" name="author" id="author" value="{{ $article->author }}">
                @error('title')
                    <p class="errortext">{{ $errors->first('author') }}</p> 
                @enderror
            </div>
        </div>
        <div class="form-group">
            <label class="label" for="photopath">Path to Photo</label>
            <div class="control">
                <input class="form-control @error('photopath') errorborder @enderror" type="text" name="photopath" id="photopath" value="{{ $article->photopath }}">
                @error('title')
                    <p class="errortext">{{ $errors->first('photopath') }}</p> 
                @enderror
            </div>
        </div>
        <div class="form-group">
            <label class="label" for="excerpt">Excerpt</label>
            <div class="control">
                <textarea class="form-control @error('excerpt') errorborder @enderror" name="excerpt" id="excerpt">{{ $article->excerpt }}</textarea>
                @error('excerpt')
                    <p class="errortext">{{ $errors->first('excerpt') }}</p> 
                @enderror
            </div>
        </div>
        <div class="form-group">
            <label class="label" for="body">Body</label>
            <div class="control">
                <textarea class="form-control @error('body') errorborder @enderror" name="body" id="body">{{ $article->body }}</textarea>
                @error('body')
                    <p class="errortext">{{ $errors->first('body') }}</p> 
                @enderror
            </div>
        </div>
        <div class="control">
            <button class="btn btn-primary" type="submit">Submit</button>
        </div>
    </form>
</div>
</div>
@endsection
Is there anything else you need to see?



  [1]: https://laracasts.com/series/laravel-6-from-scratch/episodes/33?autoplay=true

【问题讨论】:

  • 我认为您的表单操作中有错误的 url。试试&lt;form action="{{ url('/articles/'.$article-&gt;id) }}"&gt;
  • @porloscerros - 我尝试了您的建议并得到: InvalidArgumentException HTTP 状态代码“2”无效。 localhost:8000/articles/2 。 2 是我尝试更新的记录的键。
  • 查看您的控制器更新方法。你有一个逗号,而不是一个点 return redirect('/articles/', $article-&gt;id); 应该是 return redirect('/articles/'.$article-&gt;id);

标签: laravel


【解决方案1】:

你的 Laravel 路由是:

Route::put('/articles/{article}', 'ArticlesController@update');

所以您的表单操作 url 应该与该 uri 匹配:

<form action="{{ url('/articles/'.$article->id) }}">

{article} 参数是记录 ID(您可以在文档 here 中了解更多信息)。

然后在你的控制器update() 方法中,你有:

return redirect('/articles/', $article->id);

这意味着重定向到/articles,状态码为$article-&gt;id(您可以在文档here 中了解更多信息)。我认为您正在尝试重定向到显示路线,即:

Route::get('/articles/{article}', 'ArticlesController@show');

所以将,(逗号)更改为.(点)到concatenate 带有uri 的文章ID:

return redirect('/articles/' . $article->id);

【讨论】:

  • 宾果游戏!把逗号改成句号就行了! (当然,我也必须正确执行操作,但我之前已修复,但没有效果。)我从视频屏幕复制了重定向代码,在使用原始字段后的某个时间点,我必须重新输入它并输入了逗号而不是点。非常感谢!
  • 太好了,很高兴贡献。对不起,我的英语不好......正确的词是“点”而不是“点”,是吗?快乐编码 ;-)
  • 英文中,我们指的是'.'当它是像“4.275”这样的十进制数字时,当我们谈论句子的标点符号时(或其他一些情况),当我们拼写 IP 地址之类的东西时,通常称它为点。跨度>
【解决方案2】:

/articles 表单中的路由,但是你的更新路由应该是/articles/{article}

试试这个:

<form method="POST" action="/articles/{{ $article->id }}">

【讨论】:

  • 我尝试了您的建议并得到: InvalidArgumentException HTTP 状态代码“2”无效。 localhost:8000/articles/2 。 2 是我尝试更新的记录的键。
  • 我猜它在重定向功能中,尝试将重定向行更改为return redirect('/articles/', $article-&gt;id);。并检查文章是否更新。
猜你喜欢
  • 2014-08-14
  • 2017-05-04
  • 1970-01-01
  • 2021-07-08
  • 2016-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-21
相关资源
最近更新 更多