S.M_Emamian
S.M_Emamian

Reputation: 17383

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 ( {} ) !

Upvotes: 0

Views: 302

Answers (1)

Salim Djerbouh
Salim Djerbouh

Reputation: 11034

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, e.g., 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');

Upvotes: 1

Related Questions