Laravel 表单提交没有走正确的路线?

Waq*_*mad 1 php laravel

我已经安装了最新的laravel。我做了这个简单的表格。我想创建,post但是当我提交时,它转到了localhost/post 错误的地方URL。实际URLhttp://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>
Run Code Online (Sandbox Code Playgroud)

我的路线

 Route::get('/' ,'PostController@index');

Route::get('/posts/create', 'PostController@create');
Route::post('/post','PostController@store');
Run Code Online (Sandbox Code Playgroud)

Nab*_*han 5

您的简短解决方案是使用action="/laravel_practice/post"action="/laravel_practice/public/post"取决于您想要访问的网址。

然而,这是一个不好的做法。您应该使用路线名称。为此,请为路线指定任何名称,如下所示,

Route::post('/post','PostController@store')->name('post.store');
Run Code Online (Sandbox Code Playgroud)

然后在视图中你应该使用,

<form method="post" action="{{ route('post.store') }}">
Run Code Online (Sandbox Code Playgroud)

要了解有关命名路由的更多信息,您可以浏览此文档