简体   繁体   中英

api resource returns 404 Not Found

these are my api:

$api = app('Dingo\Api\Routing\Router');

$api->version('v1', ['as' => 'admin',
                     'middleware' => 'api.auth',
                     'namespace' => 'Modules\OrgUnit\Http\Controllers'], function ($api) {

    $api->group(['prefix' => 'admin/org_units', 'as' => 'org_units'], function ($api) {

            $api->get('/datatable', 'OrgUnitController@datatable')->name('.datatable');

            $api->resource('/', 'OrgUnitController');
    });

});

but api/v1/admin/org_units/datatable works find but api/v1/admin/org_units/3 returns 404 Not Found message.

my routes list:

|      | GET|HEAD                       | /api/v1/admin/org_units/datatable                                                                   | admin.org_units.datatable                                                               | Modules\OrgUnit\Http\Controllers\OrgUnitController@datatable                                                       | Yes       | v1         |          |            |
|      | GET|HEAD                       | /api/v1/admin/org_units                                                                             | admin.org_units.index                                                                   | Modules\OrgUnit\Http\Controllers\OrgUnitController@index                                                           | Yes       | v1         |          |            |
|      | POST                           | /api/v1/admin/org_units                                                                             | admin.org_units.store                                                                   | Modules\OrgUnit\Http\Controllers\OrgUnitController@store                                                           | Yes       | v1         |          |            |
|      | GET|HEAD                       | /api/v1/admin/org_units/{}                                                                          | admin.org_units.show                                                                    | Modules\OrgUnit\Http\Controllers\OrgUnitController@show                                                            | Yes       | v1         |          |            |
|      | PUT|PATCH                      | /api/v1/admin/org_units/{}                                                                          | admin.org_units.update                                                                  | Modules\OrgUnit\Http\Controllers\OrgUnitController@update                                                          | Yes       | v1         |          |            |
|      | DELETE                         | /api/v1/admin/org_units/{}                                                                          | admin.org_units.destroy                                                                 | Modules\OrgUnit\Http\Controllers\OrgUnitController@destroy                                                         | Yes       | v1         |          |            |

I think the end of my route list should be a org_unit parameters but the result is empty ( {} ) !

Pass the missing parameter when creating the route

$api->resource('/{org_unit}', 'Modules\OrgUnit\Http\Controllers\OrgUnitController');

Make sure to have a route key name in your OrgUnit model

public function getRouteKeyName()
{
   return 'id';
}

From the docs

You can also register resources and controllers using the respective methods.

Note that you must specify the full namespace to the controller, eg, App\Http\Controllers.

You can also pass parameters like so See This

$api->resource('org_units', 'Modules\OrgUnit\Http\Controllers\OrgUnitController', [
       'parameters' => ['org_unit' => 'application'],
])->middlware('bindings');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM