簡體   English   中英

Laravel:此路由不支持 POST 方法

[英]Laravel: The POST method is not supported for this route

我對 Laravel 有點陌生,我為我的頁面制作了一個表單,用戶可以添加新圖像,這個表單位於create.blade.php中:

<form action="/p" enctype="multipart/form-data" method="post">
@csrf
<div class="row">
    <div class="col-8 offset-2">
        <div class="row">
            <h1>Add New Post</h1>
        </div>
        <div class="form-group row">
            <label for="caption" class="col-md-4 col-form-label">Post Caption</label>

              
            <input id="caption" 
                type="text" 
                class="form-control @error('caption') is-invalid @enderror" 
                name="caption" 
                value="{{ old('caption') }}" 
                autocomplete="caption" autofocus>

                @error('caption')
                    <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                @enderror
             
        </div>
        <div class="row">
            <label for="image" class="col-md-4 col-form-label">Post Image</label> 
            <input type="file" class="form-control-file" id="image" name="image">
            @error('image')
                <span class="invalid-feedback" role="alert">
                    <strong>{{ $message }}</strong>
                </span>
            @enderror
        </div>
        <div class="row pt-4">
            <button class="btn btn-primary">Add New Post</button>
        </div>
    </div>
</div>

這是web.php (路由)文件:

Route::get('/p/create','PostsController@create');
Route::get('/p','PostsController@store');

Route::get('/profile/{user}', 'ProfilesController@index')->name('profile.show');

如您所見,它指的是PostController.php這是這樣的:

class PostsController extends Controller
{
    public function create()
    {
        return view('posts.create');
    }
    public function store()
    {
        dd(request()->all());
    }
}

另外我執行命令php artisan route:list就是這樣:

打印屏幕

那么這里出了什么問題呢? 我搜索了很多,但找不到任何有用的東西。 所以如果你知道如何解決這個問題,請告訴我。

提前致謝

您正在將請求發送到服務器,因此您需要將 HTTP 請求設置為 post not get,就像這樣

Route::post('/p','PostsController@store');

您需要添加一個POST路由

Route::post('/p','PostsController@store');
  1. 在 create.blade.php 形式方法是POST但在 web.php 是Route::get('/p','PostsController@store'); 所以你應該改變Route::post('/p','PostsController@store')而不是Route::get('/p','PostsController@store')

  2. 在 Controller

     use Illuminate\Http\Request; class PostsController extends Controller { public function create() { return view('posts.create'); } public function store(Request $request) { dd($request->input('image')); } }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM