Laravel嵌套资源上的自定义控制器功能

时间:2018-08-09 13:22:21

标签: laravel laravel-5.6

我有一个可以正常工作的 web.php

Route::delete('admin/bursary_providers/destroy_many', 'AdminBursaryProviderController@destroyMany')->name('bursary_providers.destroy_many');
Route::resource('admin/bursary_providers', 'AdminBursaryProviderController');

结果:

    | POST      | admin/bursary_providers                               | bursary_providers.store           | App\Http\Controllers\AdminBursaryProviderController@store              | web,admin    |
    | GET|HEAD  | admin/bursary_providers                               | bursary_providers.index           | App\Http\Controllers\AdminBursaryProviderController@index              | web,admin    |
    | GET|HEAD  | admin/bursary_providers/create                        | bursary_providers.create          | App\Http\Controllers\AdminBursaryProviderController@create             | web,admin    |
    | DELETE    | admin/bursary_providers/destroy_many                  | bursary_providers.destroy_many    | App\Http\Controllers\AdminBursaryProviderController@destroyMany        | web,admin    |
    | DELETE    | admin/bursary_providers/{bursary_provider}            | bursary_providers.destroy         | App\Http\Controllers\AdminBursaryProviderController@destroy            | web,admin    |
    | PUT|PATCH | admin/bursary_providers/{bursary_provider}            | bursary_providers.update          | App\Http\Controllers\AdminBursaryProviderController@update             | web,admin    |
    | GET|HEAD  | admin/bursary_providers/{bursary_provider}            | bursary_providers.show            | App\Http\Controllers\AdminBursaryProviderController@show               | web,admin    |
    | GET|HEAD  | admin/bursary_providers/{bursary_provider}/edit       | bursary_providers.edit            | App\Http\Controllers\AdminBursaryProviderController@edit               | web,admin    |

但是然后我有一个嵌套的资源,并且还想要' destroy_many ':

Route::delete('admin/bursary_providers/destroy_many', 'AdminBursaryProviderController@destroyMany')->name('bursary_providers.destroy_many');
Route::resource('admin/bursary_providers', 'AdminBursaryProviderController');
Route::delete('admin/bursary_providers/contacts/destroy_many', 'AdminBursaryProviderContactController@destroyMany')->name('bursary_providers.contacts.destroy_many');
Route::resource('admin/bursary_providers.contacts', 'AdminBursaryProviderContactController');

结果

    | POST      | admin/bursary_providers                                            | bursary_providers.store                 | App\Http\Controllers\AdminBursaryProviderController@store              | web,admin    |
    | GET|HEAD  | admin/bursary_providers                                            | bursary_providers.index                 | App\Http\Controllers\AdminBursaryProviderController@index              | web,admin    |
    | DELETE    | admin/bursary_providers/contacts/destroy_many                      | bursary_providers.contacts.destroy_many | App\Http\Controllers\AdminBursaryProviderContactController@destroyMany | web,admin    |
    | GET|HEAD  | admin/bursary_providers/create                                     | bursary_providers.create                | App\Http\Controllers\AdminBursaryProviderController@create             | web,admin    |
    | DELETE    | admin/bursary_providers/destroy_many                               | bursary_providers.destroy_many          | App\Http\Controllers\AdminBursaryProviderController@destroyMany        | web,admin    |
    | GET|HEAD  | admin/bursary_providers/{bursary_provider}                         | bursary_providers.show                  | App\Http\Controllers\AdminBursaryProviderController@show               | web,admin    |
    | DELETE    | admin/bursary_providers/{bursary_provider}                         | bursary_providers.destroy               | App\Http\Controllers\AdminBursaryProviderController@destroy            | web,admin    |
    | PUT|PATCH | admin/bursary_providers/{bursary_provider}                         | bursary_providers.update                | App\Http\Controllers\AdminBursaryProviderController@update             | web,admin    |
    | POST      | admin/bursary_providers/{bursary_provider}/contacts                | bursary_providers.contacts.store        | App\Http\Controllers\AdminBursaryProviderContactController@store       | web,admin    |
    | GET|HEAD  | admin/bursary_providers/{bursary_provider}/contacts                | bursary_providers.contacts.index        | App\Http\Controllers\AdminBursaryProviderContactController@index       | web,admin    |
    | GET|HEAD  | admin/bursary_providers/{bursary_provider}/contacts/create         | bursary_providers.contacts.create       | App\Http\Controllers\AdminBursaryProviderContactController@create      | web,admin    |
    | PUT|PATCH | admin/bursary_providers/{bursary_provider}/contacts/{contact}      | bursary_providers.contacts.update       | App\Http\Controllers\AdminBursaryProviderContactController@update      | web,admin    |
    | GET|HEAD  | admin/bursary_providers/{bursary_provider}/contacts/{contact}      | bursary_providers.contacts.show         | App\Http\Controllers\AdminBursaryProviderContactController@show        | web,admin    |
    | DELETE    | admin/bursary_providers/{bursary_provider}/contacts/{contact}      | bursary_providers.contacts.destroy      | App\Http\Controllers\AdminBursaryProviderContactController@destroy     | web,admin    |
    | GET|HEAD  | admin/bursary_providers/{bursary_provider}/contacts/{contact}/edit | bursary_providers.contacts.edit         | App\Http\Controllers\AdminBursaryProviderContactController@edit        | web,admin    |
    | GET|HEAD  | admin/bursary_providers/{bursary_provider}/edit                    | bursary_providers.edit                  | App\Http\Controllers\AdminBursaryProviderController@edit               | web,admin    |

如果我尝试这样做:

Route::delete('admin/bursary_providers/destroy_many', 'AdminBursaryProviderController@destroyMany')->name('bursary_providers.destroy_many');
Route::resource('admin/bursary_providers', 'AdminBursaryProviderController');
Route::delete('admin/bursary_providers.contacts.destroy_many', 'AdminBursaryProviderContactController@destroyMany')->name('bursary_providers.contacts.destroy_many');
Route::resource('admin/bursary_providers.contacts', 'AdminBursaryProviderContactController');

它也不正确:

    | POST      | admin/bursary_providers                                            | bursary_providers.store                 | App\Http\Controllers\AdminBursaryProviderController@store              | web,admin    |
    | GET|HEAD  | admin/bursary_providers                                            | bursary_providers.index                 | App\Http\Controllers\AdminBursaryProviderController@index              | web,admin    |
    | DELETE    | admin/bursary_providers.contacts.destroy_many                      | bursary_providers.contacts.destroy_many | App\Http\Controllers\AdminBursaryProviderContactController@destroyMany | web,admin    |
    | GET|HEAD  | admin/bursary_providers/create                                     | bursary_providers.create                | App\Http\Controllers\AdminBursaryProviderController@create             | web,admin    |
    | DELETE    | admin/bursary_providers/destroy_many                               | bursary_providers.destroy_many          | App\Http\Controllers\AdminBursaryProviderController@destroyMany        | web,admin    |
    | GET|HEAD  | admin/bursary_providers/{bursary_provider}                         | bursary_providers.show                  | App\Http\Controllers\AdminBursaryProviderController@show               | web,admin    |
    | DELETE    | admin/bursary_providers/{bursary_provider}                         | bursary_providers.destroy               | App\Http\Controllers\AdminBursaryProviderController@destroy            | web,admin    |
    | PUT|PATCH | admin/bursary_providers/{bursary_provider}                         | bursary_providers.update                | App\Http\Controllers\AdminBursaryProviderController@update             | web,admin    |
    | POST      | admin/bursary_providers/{bursary_provider}/contacts                | bursary_providers.contacts.store        | App\Http\Controllers\AdminBursaryProviderContactController@store       | web,admin    |
    | GET|HEAD  | admin/bursary_providers/{bursary_provider}/contacts                | bursary_providers.contacts.index        | App\Http\Controllers\AdminBursaryProviderContactController@index       | web,admin    |
    | GET|HEAD  | admin/bursary_providers/{bursary_provider}/contacts/create         | bursary_providers.contacts.create       | App\Http\Controllers\AdminBursaryProviderContactController@create      | web,admin    |
    | PUT|PATCH | admin/bursary_providers/{bursary_provider}/contacts/{contact}      | bursary_providers.contacts.update       | App\Http\Controllers\AdminBursaryProviderContactController@update      | web,admin    |
    | GET|HEAD  | admin/bursary_providers/{bursary_provider}/contacts/{contact}      | bursary_providers.contacts.show         | App\Http\Controllers\AdminBursaryProviderContactController@show        | web,admin    |
    | DELETE    | admin/bursary_providers/{bursary_provider}/contacts/{contact}      | bursary_providers.contacts.destroy      | App\Http\Controllers\AdminBursaryProviderContactController@destroy     | web,admin    |
    | GET|HEAD  | admin/bursary_providers/{bursary_provider}/contacts/{contact}/edit | bursary_providers.contacts.edit         | App\Http\Controllers\AdminBursaryProviderContactController@edit        | web,admin    |
    | GET|HEAD  | admin/bursary_providers/{bursary_provider}/edit                    | bursary_providers.edit                  | App\Http\Controllers\AdminBursaryProviderController@edit               | web,admin    |

这里的最终目标是拥有以下内容,我该如何实现呢?:

| DELETE    | admin/bursary_providers/{bursary_provider}/contacts/{contact}/delete_many

1 个答案:

答案 0 :(得分:0)

不确定这样做是否正确,但是输出显示正确吗?

Route::delete('admin/bursary_providers/{bursary_provider}/contacts/destroy_many', 'AdminBursaryProviderContactController@destroyMany')->name('bursary_providers.contacts.destroy_many');

导致:

    | DELETE    | admin/bursary_providers/{bursary_provider}/contacts/destroy_many   | bursary_providers.contacts.destroy_many | App\Http\Controllers\AdminBursaryProviderContactController@destroyMany | web,admin    |