【问题标题】:GET method is not supported for the route Laravel 8路由 Laravel 8 不支持 GET 方法
【发布时间】:2021-06-19 00:43:20
【问题描述】:

我正在测试我的删除功能,但如何将它集成到按钮这里是代码

profile.blade

    <div class="modal-body">Select the delete button to continue</div>
                <div class="modal-footer">
                    <button class="btn btn-primary" type="button" data-dismiss="modal">Cancel</button>
                    <button class="btn btn-secondary"type="button" href="/delete">Delete</a>
                </div>
            </div>

这是我想使用该功能的地方

这里是路线

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\{
    DashboardController,
    HomeController,
    LoginController,
    RegisterController,
    FileuploadController,
    MemberController
};

Route::middleware(['guest'])->group(function() {
    // (url_attern, [Controller_name, method_name])->name(alias)
    Route::get('/', [HomeController::class, 'index'])->name('home.index');
    Route::get('/attorney', [HomeController::class, 'attorney'])->name('home.attorney');
    Route::get('/service', [HomeController::class, 'service'])->name('home.service');


    Route::get('/login', [LoginController::class, 'login'])->name('login.login');
    Route::post('/login', [LoginController::class, 'auth'])->name('login.auth');
    Route::get('/register', [RegisterController::class, 'signin'])->name('register.signin');
    Route::post('/register', [RegisterController::class, 'register'])->name('register.register');
});

Route::middleware(['auth'])->group(function() {
    Route::get('/logout', [LoginController::class, 'logout'])->name('login.logout');
    Route::get('/dashboard', [DashboardController::class, 'dashboard'])->name('dashboard.dashboard');
    Route::get('/profile', [DashboardController::class, 'profile'])->name('dashboard.profile');
    Route::get('/journal', [DashboardController::class, 'journal'])->name('dashboard.journal');
    Route::get('/files', [DashboardController::class, 'files'])->name('dashboard.files');
    Route::post('/files', [FileuploadController::class, 'files'])->name('dashboard.files');
    Route::get('/directory', [DashboardController::class, 'directory'])->name('dashboard.directory');
    Route::get('/calendar', [DashboardController::class, 'calendar'])->name('dashboard.calendar');
    Route::post('/delete', [DashboardController::class, 'delete'])->name('dashboard.delete');
});

还有功能

  public function delete(){
        $user = \User::find(Auth::user()->id);

        Auth::logout();
    
        if ($user->delete()) {
    
             return Redirect::route('/')->with('global', 'Your account has been deleted!');
        }
    
    }

如何使用该功能删除当前登录的用户 我的路线已经在 post 进行删除,但它表明该路线不支持 GET 方法

【问题讨论】:

  • 不用$user = \User::find(Auth::user()-&gt;id);,你可以$user = Auth::user();
  • 我应该在按钮中添加什么以使其起作用?

标签: php laravel file crud laravel-8


【解决方案1】:

路由来自 POST 动词,所以需要在表单中插入按钮:

<div class="modal-body">Select the delete button to continue</div>
   <div class="modal-footer">
      <button class="btn btn-primary" type="button" data-dismiss="modal">Cancel</button>
      <form action="{{ route('dashboard.delete') }}" method="POST">
      {{ csrf() }}
         <button class="btn btn-secondary" type="submit">Delete</button>
      </form>
   </div>
</div>

【讨论】:

    【解决方案2】:

    你必须在一个带有 POST 动词的表单中实现它,这样

    <form action="/logout" method="post">
        {{csrf_field()}}
        <a href='#' onclick='this.parentNode.submit(); return false;' class="nav-link">
           <i class="nav-icon fa fa-sign-out-alt"></i>
           <p>Log out</p>
       </a>
    </form>
    

    【讨论】:

    • 抱歉,我一定是打错字了,应该是删除功能而不是注销
    猜你喜欢
    • 2021-08-05
    • 2021-01-10
    • 1970-01-01
    • 2021-10-07
    • 2021-05-08
    • 2021-03-22
    • 2020-11-27
    • 2019-10-12
    相关资源
    最近更新 更多