php - Laravel 表单提交没有走正确的路线?
问题描述
我已经安装了最新的 laravel。我做了这个简单的表格。我想创建post
,但是当我提交它时,localhost/post
这是错误的URL
。实际URL
是http://localhost/laravel_practice/
'
形式
<form method="post" action="/post">
<div class="form-group">
<label>Title</label>
<input type="text" name="title" class="form-control" placeholder="Enter Title Here">
</div>
<div class="form-group">
<label>Body</label>
<textarea name="body" class="form-control" placeholder="Enter the body"></textarea>
</div>
<div class="form-group">
<input type="submit" name="sumit" class="btn btn-primary" value="Publish">
</div>
我的路线
Route::get('/' ,'PostController@index');
Route::get('/posts/create', 'PostController@create');
Route::post('/post','PostController@store');
解决方案
您的简短解决方法是使用action="/laravel_practice/post"
或action="/laravel_practice/public/post"
取决于您要使用的网址。
但是,这是一个不好的做法。您应该使用路线名称。为此,请为如下路线命名,
Route::post('/post','PostController@store')->name('post.store');
然后考虑到你应该使用,
<form method="post" action="{{ route('post.store') }}">
要阅读有关命名路由的更多信息,您可以阅读此文档。