Я пытаюсь понять, почему я получаю эту ошибку при отправке формы. Я нашел обходной путь, но я не обязательно понимаю, почему обходной путь необходим и если он вызовет проблемы позже. Я публикую это как для понимания ошибки, так и для помощи другим, кто сталкивается с подобной проблемой.
Текст, выделенный жирным шрифтом ниже, - это то, что я сделал, чтобы получить эту функцию, добавив «/ posts / create». Однако в Laravel документах путь к хранилищу отображается как '/ post', 'PostController@store'. заранее.
Интернет. php
<?php
use Illuminate\Support\Facades\Route;
use App\Mail\WelcomeMail;
Route::get('/', function () {
return view('welcome');
});
Route::get('/email', function () {
return new WelcomeMail();
});
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/contact/create', 'ContactController@create');
Route::post('/contact', 'ContactController@store');
Route::get('/posts', 'PostController@index');
Route::get('/posts/create', 'PostController@create');
Route::post('/posts/create', 'PostController@store'); **<-workaround**
Route::get('/posts/{post}', 'PostController@show');
Route::get('/posts/{post}/edit', 'PostController@edit');
Route::patch('/posts/{post}', 'PostController@update');
Route::delete('/posts/{post}', 'PostController@destroy');
Форма
<div class="container">
<div class="row justify-content-cemter">
<div class="col-md-8">
<div class="card">
<div class="card-header">Create New Post</div>
<div class="card-body">
<form action="/posts/create" method="post">
@csrf
<div class="form-group">
<label for="title">Title</label>
<input name="title" type="text" class="form-control" id="title" aria-describedby="titleHelp" placeholder="Enter Title" autocomplete="off" value="{{ old('title') ?? $post->title }}">
<small id="titleHelp" class="form-text text-muted">Give your post a title that will describe your post easily</small>
@error ('title') <p style="color:red;">{{ $message }}</p>
@enderror
</div>
<div class="form-group">
<label for="body">Body</label>
<input name="body" type="text" class="form-control" id="body" aria-describedby="bodyHelp" placeholder="Enter Post Body" autocomplete="off" value="{{ old('body') ?? $post->body }}">
<small id="bodyHelp" class="form-text text-muted">Enter as much detail you'd like!</small>
@error ('body') <p style="color:red;">{{ $message }}</p>
@enderror
</div>
<button type="submit" class="btn btn-primary">Create Post</button>
</form>
</div>
</div>
</div>
</div>
</div>
PostContorller
<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$posts = Post::all();
return view('post.index', compact('posts'));
}
public function create()
{
$post = new Post();
return view('post.create', compact ('post'));
}
public function store()
{
Post::create($this->validatedData());
return redirect('/posts');
}
public function show(Post $post)
{
return view('post.show', compact('post'));
}
public function edit(Post $post)
{
return view('post.edit', compact('post'));
}
public function update(Post $post)
{
$post->update($this->validatedData());
return \redirect('/posts');
}
public function destroy(Post $post)
{
$post->delete();
return redirect('/posts');
}
protected function validatedData()
{
return request()->validate([
'title' => 'required',
'body' => 'required'
//'photograph'=> nullable
//'gif'=> nullable
]);
}
}
Маршрут: список
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+------------------------+------------------+------------------------------------------------------------------------+--------------+
| | GET|HEAD | / | | Closure | web |
| | GET|HEAD | about | | Closure | web |
| | GET|HEAD | api/user | | Closure | api,auth:api |
| | POST | contact | | App\Http\Controllers\ContactController@store | web |
| | GET|HEAD | contact/create | | App\Http\Controllers\ContactController@create | web |
| | GET|HEAD | email | | Closure | web |
| | GET|HEAD | home | home | App\Http\Controllers\HomeController@index | web,auth |
| | GET|HEAD | login | login | App\Http\Controllers\Auth\LoginController@showLoginForm | web,guest |
| | POST | login | | App\Http\Controllers\Auth\LoginController@login | web,guest |
| | POST | logout | logout | App\Http\Controllers\Auth\LoginController@logout | web |
| | GET|HEAD | password/confirm | password.confirm | App\Http\Controllers\Auth\ConfirmPasswordController@showConfirmForm | web,auth |
| | POST | password/confirm | | App\Http\Controllers\Auth\ConfirmPasswordController@confirm | web,auth |
| | POST | password/email | password.email | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail | web |
| | POST | password/reset | password.update | App\Http\Controllers\Auth\ResetPasswordController@reset | web |
| | GET|HEAD | password/reset | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web |
| | GET|HEAD | password/reset/{token} | password.reset | App\Http\Controllers\Auth\ResetPasswordController@showResetForm | web |
| | GET|HEAD | posts | | App\Http\Controllers\PostController@index | web,auth |
| | GET|HEAD | posts/create | | App\Http\Controllers\PostController@create | web,auth |
| | POST | posts/create | | App\Http\Controllers\PostController@store | web,auth |
| | GET|HEAD | posts/{post} | | App\Http\Controllers\PostController@show | web,auth |
| | PATCH | posts/{post} | | App\Http\Controllers\PostController@update | web,auth |
| | DELETE | posts/{post} | | App\Http\Controllers\PostController@destroy | web,auth |
| | GET|HEAD | posts/{post}/edit | | App\Http\Controllers\PostController@edit | web,auth |
| | POST | register | | App\Http\Controllers\Auth\RegisterController@register | web,guest |
| | GET|HEAD | register | register | App\Http\Controllers\Auth\RegisterController@showRegistrationForm | web,guest |
+--------+----------+------------------------+------------------+------------------------------------------------------------------------+--------------+