【问题标题】:Route [dashboard.update] not defined. while it is defined in routes (routs/web.php) file路线 [dashboard.update] 未定义。虽然它是在路由(routs/web.php)文件中定义的
【发布时间】:2017-05-21 16:27:21
【问题描述】:

这是我的 laravel 路由文件代码和所有其他路由都运行良好,但“更新”路由给出了一个错误,即“路由 [dashboard.update] 未定义” 这是路线代码

Route::group(['middleware' => 'auth', 'prefix' => 'dashboard'],function (){

// get routes
Route::get('/', 'HomeController@index')->name('dashboard.index');
Route::get('add', 'HomeController@addNotice')->name('dashboard.add');
Route::get('{id}/edit', 'HomeController@editNotice')->name('dashboard.edit');
Route::get('{id}', 'HomeController@showNotice')->name('dashboard.show');

// post routes
Route::post('add', 'HomeController@storeNotice')->name('dashboard.store');
Route::post('{id}','HomeController@updateNotice')->name('dashboard.update'); //error here
Route::post('{id}', 'HomeController@deleteNotice ')->name('dashboard.delete');
});

这是HomeController返回视图的代码

<form action="{{ route('dashboard.update',['noticeId' => $noticeId->id]) }}" method="POST" style="padding-left: 10px; padding-right: 10px;">
            {{ csrf_field() }}
            <div class="row">
                <div class="form-group">
                    <input type="text" class="form-control" name="noticeTitle" placeholder="Give Title to Notice" value="{{ $noticeId->title }}">
                </div>
            </div>

            <div class="row">
                <div class="form-group">
                    <textarea name="noticeBody" cols="30" rows="8" class="form-control" placeholder="Add Notice Details Here" style="resize: none">{{ $noticeId->body }}</textarea>
                </div>
            </div>

            <div class="row">
                <div class="form-group">
                    <input type="submit" name="editNotice" value="Save Changes" class="btn btn-info btn-block btn-sm">
                    <input type="hidden" name="_method" value="PUT">
                </div>
            </div>
        </form>

【问题讨论】:

  • 谢谢@FPJ,它真的很有帮助,我使用 post 方法进行更新和删除,而他们有 'put' 和 'delete' 方法。

标签: php laravel routes


【解决方案1】:
Route::post('{id}','HomeController@updateNotice')->name('dashboard.update');

到

Route::put('{id}','HomeController@updateNotice')->name('dashboard.update');

【讨论】:

    【解决方案2】:

    将您的路线类型更改为PUT 或PATCH

    Route::patch('{id}','HomeController@updateNotice')->name('dashboard.update');
    

    这是你的表格

    {!! method_field('PATCH') !!}
    

    这里的错误是因为您的删除路线正在覆盖更新路线,因为它们的类型和路径相同。通常对于删除路线,您应该使用“DELETE”方法。

    【讨论】:

    • 您不能使用表单进行修补/放置!!
    • The method_field function generates an HTML hidden input field containing the spoofed value of the form's HTTP verb.,所以这会和Route::patch冲突
    • 这只是一种变通方法,不会执行补丁请求
    • @hassan 这就是 laravel 表单和路由的工作方式。感谢您的反对。
    猜你喜欢
    • 2018-12-19
    • 2017-06-04
    • 1970-01-01
    • 2021-03-17
    • 2019-09-29
    • 2018-07-13
    • 1970-01-01
    • 2019-02-15
    相关资源
    最近更新 更多