【问题标题】:Laravel: REJECT ROUTE not defined but exists in web.phpLaravel:REJECT ROUTE 未定义但存在于 web.php 中
【发布时间】:2023-03-02 21:21:01
【问题描述】:

我的日历控制器中有一个拒绝功能,但是每当我重定向到视图页面时,它都会显示一条错误消息,指出我的路线未定义。

我已尝试重新排列和重命名路线,但仍显示错误。

这是我的表格:

{!! Form::open(['url' => route('therapist.reject.appointment', $bookingRequest), 'method' => 'delete', 'onsubmit' => 'javascript:return confirm("Are you sure?")']) !!}
                                <button type="submit" class="btn btn-warning btn-block">Reject this appointment</button>
                                {{csrf_field()}}
                            {!! Form::close() !!}

这是我的路线。显示的其他路线运行良好:

Route::get('therapist-calendar/{bookingRequest}', 'TherapistCalander')->name('therapist.calendar');

    Route::post('therapist-calendar/{bookingRequest}',
        'TherapistCalander@saveAppointment')->name('therapist.book.appointment');

    Route::patch('therapist-calendar/{bookingRequest}', 
        'TherapistCalander@finishedAppointment')->name('therapist.finish.appointment');

    Route::delete('therapist-calendar/{bookingRequest}',
    'TherapistCalander@rejectAppointment')->name('therapist.reject.appointment');

    Route::delete('therapist-calendar/{bookingRequest}', 
        'TherapistCalander@cancelAppointment')->name('therapist.cancel.appointment');

最后,我的功能:

public function rejectAppointment(Request $request, BookingRequest $bookingRequest)
    {
        $bookingRequest->reject();

        return redirect()->back()->with('rejectStatus', true);
    }

该按钮所在的视图页面应该能够在日历视图旁边显示拒绝和结束按钮。

编辑 后续问题:可能是因为路线彼此相似吗?如果是这样,我该如何解决这个问题?

【问题讨论】:

  • 你在使用所有标记版本的 Laravel 吗?如果没有,你能更新问题标签吗?
  • 此外,您应该查看资源路由 - 这将使您的路由更简单并处理一些样板工作。
  • @Adam 对此感到抱歉!编辑:D
  • 即使我没有使用资源控制器,是否可以使用资源路由?
  • 是的。资源控制器只是命名每个函数的标准方式 - 您可以复制其中的一部分(或全部)而无需通过 php artisan make:controller --resource 创建它。

标签: laravel laravel-5 laravel-5.6 laravel-routing laravel-route


【解决方案1】:

尝试更改 Reject 和 Cancel 的 url 字符串,因为它是相似的。

Route::delete(
    'therapist-calendar/{bookingRequest}/delete',
    'TherapistCalander@rejectAppointment'
)->name('therapist.reject.appointment');

Route::delete(
    'therapist-calendar/{bookingRequest}', 
    'TherapistCalander@cancelAppointment'
)->name('therapist.cancel.appointment');

【讨论】:

    【解决方案2】:

    把你的代码改成

        {!! Form::open(['url' => route('therapist.reject.appointment', ['bookingRequest' => $bookingRequest]), 'method' => 'delete', 'onsubmit' => 'javascript:return confirm("Are you sure?")']) !!}
          {{csrf_field()}}
          <button type="submit" class="btn btn-warning btn-block">Reject this appointment</button>
       {!! Form::close() !!}
    

    路由参数作为数组传递,应该可以正常工作。参考doc

    你能试试这个代码吗

    <form action="{{ route('therapist.reject.appointment', ['bookingRequest' => $bookingRequest]) }}" method="POST">
        @method('DELETE')
        @csrf
        <button type="submit" class="btn btn-warning btn-block">Reject this appointment</button>
    </form>
    

    【讨论】:

    • 路由中的单个参数不强制传递数组。
    • @jogesh_pi 添加参考链接对我和未来的访问者来说将是一个很大的帮助,您能否为文档添加参考链接?
    • @DanielaEchavez 你能试试新代码吗,我已经发布并告诉我你会得到的新结果/错误?
    • @PrafullaKumarSahu 我更改了链接。我只有在发布更新后才能看到您的帖子。不过谢谢!
    • @DanielaEchavez 没问题的朋友,更重要的是以正确的方式解决问题,而不是通过变通,如果你有解决方案,总是好的。
    【解决方案3】:

    更新

    问题已修复

    我意识到,因为他们有相似的链接,web.php 发现它很混乱,所以它没有读取这条路线。

    这就是我改变路线的原因:

      Route::delete('therapist-calendar/{bookingRequest}',
    'TherapistCalander@rejectAppointment')->name('therapist.reject.appointment');
    

    到这里:

    Route::delete('doReject/{bookingRequest}',
        'TherapistCalander@rejectAppointment')->name('therapist.reject.appointment');
    

    【讨论】:

      猜你喜欢
      • 2019-10-01
      • 1970-01-01
      • 2019-04-11
      • 2017-07-07
      • 1970-01-01
      • 2017-06-11
      • 2017-12-10
      • 2021-12-30
      相关资源
      最近更新 更多