You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When setting up multiple api resource routes, only the first resource is working as expected, the others return an error similar to Unable to resolve dependency [Parameter #0 [ <required> $user_id ]] in class App\\Http\\Controllers\\UserController
Here's my web.php file:
<?php
/** @var \Laravel\Lumen\Routing\Router $router */
use App\Http\Controllers\ApiAuthController;
use App\Http\Controllers\IndexController;
use App\Http\Controllers\CompanyController;
use App\Http\Controllers\RoleController;
use App\Http\Controllers\UserAuthController;
use App\Http\Controllers\UserController;
$api = app('Dingo\Api\Routing\Router');
$api->version('v1', function ($api) {
$api->group(['middleware' => ['log']], function ($api) {
$api->post('/login', [ApiAuthController::class, 'postAuthenticate']);
$api->post('/users/login', [UserAuthController::class, 'postAuthenticate']);
});
$api->group(['middleware' => ['auth', 'log']], function ($api) {
//Root Route. This prevents any leaky details if someone tries to hit the API in the browser
$api->get('/', [IndexController::class, 'getIndex']);
//Company Routes
$api->resource('companies', CompanyController::class, [
'parameters' => ['companies' => 'company_id'],
]);
//Role Routes
$api->resource('roles', RoleController::class, [
'parameters' => ['roles' => 'role_id'],
]);
//User Auth Routes
$api->post('/users/logout', [UserAuthController::class, 'postLogout']);
$api->post('/users/refresh', [UserAuthController::class, 'postRefresh']);
//User Routes
$api->resource('users', UserController::class, [
'parameters' => ['users' => 'user_id'],
]);
});
});
If I send a GET request to /companies/5837991a-9f8d-47d7-9453-13e1b67bae35 I get a 200 OK response and the request data. However if I send a GET request to /roles/0553a087-457a-4124-8669-ca29f4c06c64 or users/46e8b3a5-085d-4f4a-bf30-411159591b56 I get the 500 Internal Server Error posted above (substitute Controller/Key details). If however, I move the API Resource statements around, for example, putting the Role Routes before the Company Routes, the request to /roles/0553a087-457a-4124-8669-ca29f4c06c64 will work, but the request to /companies/5837991a-9f8d-47d7-9453-13e1b67bae35 will fail.
The show method from CompanyController.php
public function show($company_id)
{
$company = Company::findOrFail($company_id);
return $this->response->item($company, new CompanyTransformer);
}
The show method from RoleController.php
public function show($role_id)
{
$role = Role::findOrFail($role_id);
return $this->response->item($role, new RoleTransformer);
}
While trying to track down the issue, what I'm finding is in the BoundMethod.php file, it's looking for $paramName (user_id) in the $parameters array. However, the parameters array is indexing the value as user. From what I'm finding in the Naming Resource Route Parameters it looks like this is working for the first resource, but subsequent resources are using the default singularized version.
I would expect that the various resource routes would all assign the parameter variables and load the controller as expected.
Steps to Reproduce
I believe all of the various code snippets and other items needed are all spelled out in the section above.
Possible Solutions
It looks like I could remove the specified parameters and use the singular variable in the controllers, but I like passing the variable like user_id as opposed to $user and reserve the singular for the model.
The text was updated successfully, but these errors were encountered:
Actual Behaviour
When setting up multiple api resource routes, only the first resource is working as expected, the others return an error similar to
Unable to resolve dependency [Parameter #0 [ <required> $user_id ]] in class App\\Http\\Controllers\\UserController
Here's my web.php file:
If I send a GET request to
/companies/5837991a-9f8d-47d7-9453-13e1b67bae35
I get a 200 OK response and the request data. However if I send a GET request to/roles/0553a087-457a-4124-8669-ca29f4c06c64
orusers/46e8b3a5-085d-4f4a-bf30-411159591b56
I get the 500 Internal Server Error posted above (substitute Controller/Key details). If however, I move the API Resource statements around, for example, putting the Role Routes before the Company Routes, the request to/roles/0553a087-457a-4124-8669-ca29f4c06c64
will work, but the request to/companies/5837991a-9f8d-47d7-9453-13e1b67bae35
will fail.The show method from CompanyController.php
The show method from RoleController.php
Stack Trace of Error:
While trying to track down the issue, what I'm finding is in the BoundMethod.php file, it's looking for
$paramName
(user_id
) in the $parameters array. However, the parameters array is indexing the value asuser
. From what I'm finding in the Naming Resource Route Parameters it looks like this is working for the first resource, but subsequent resources are using the default singularized version.Expected Behaviour
I would expect that the various resource routes would all assign the parameter variables and load the controller as expected.
Steps to Reproduce
I believe all of the various code snippets and other items needed are all spelled out in the section above.
Possible Solutions
It looks like I could remove the specified parameters and use the singular variable in the controllers, but I like passing the variable like user_id as opposed to $user and reserve the singular for the model.
The text was updated successfully, but these errors were encountered: