【发布时间】: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。试试
<form action="{{ url('/articles/'.$article->id) }}"> -
@porloscerros - 我尝试了您的建议并得到: InvalidArgumentException HTTP 状态代码“2”无效。 localhost:8000/articles/2 。 2 是我尝试更新的记录的键。
-
查看您的控制器更新方法。你有一个逗号,而不是一个点
return redirect('/articles/', $article->id);应该是return redirect('/articles/'.$article->id);
标签: laravel