我正在使用Laravel的Resource Controllers功能开发API,并使用Orchestral Multi-tenant Database Schema Manager作为单个数据库。
在我的控制方法中,我使用Route Model Binding将模型ID注入到路由或控制器操作中,经常查询以检索与该ID对应的模型。
我的API中的一些相关路径:
| POST | api/v1/{tenant}/fornecedores | store | App\Http\Controllers\Api\Fornecedor\FornecedorController@store | api,tenant,jwt.auth |
| GET|HEAD | api/v1/{tenant}/fornecedores | index | App\Http\Controllers\Api\Fornecedor\FornecedorController@index | api,tenant,jwt.auth |
| GET|HEAD | api/v1/{tenant}/fornecedores/count | | App\Http\Controllers\Api\Fornecedor\FornecedorController@count | api,tenant,jwt.auth |
| PUT|PATCH | api/v1/{tenant}/fornecedores/{fornecedor} | update | App\Http\Controllers\Api\Fornecedor\FornecedorController@update | api,tenant,jwt.auth |
| GET|HEAD | api/v1/{tenant}/fornecedores/{fornecedor} | show | App\Http\Controllers\Api\Fornecedor\FornecedorController@show | api,tenant,jwt.auth |
| GET|HEAD | api/v1/{tenant}/fornecedores/{fornecedor}/audits | | App\Http\Controllers\Api\Fornecedor\FornecedorController@audits | api,tenant,jwt.auth |
...
我的 API路线文件的示例:
// API V1
Route::group(['prefix' => 'v1/{tenant}', 'middleware' => 'tenant'], function () {
// Fornecedores
Route::group(['prefix' => 'fornecedores'], function () {
Route::get('count', 'Api\Fornecedor\FornecedorController@count');
Route::get('{fornecedor}/audits', 'Api\Fornecedor\FornecedorController@audits');
Route::resource('/', 'Api\Fornecedor\FornecedorController', [
'parameters' => ['' => 'fornecedor'],
'except' => ['create', 'edit', 'destroy']
]);
});
});
这些是我的 FornecedorController.php 的一些方法:
public function index()
{
return $this->fornecedorService->getFornecedoresPaginate();
}
// This is line 48 (throw below):
public function show(Fornecedor $fornecedor)
{
return $fornecedor;
}
但是,当尝试访问使用Route Model Binding的URL时,我收到以下错误:
网址: / api / v1 / 1 / fornecedores / 1
FatalThrowableError in FornecedorController.php line 48:
Type error: Argument 1 passed to App\Http\Controllers\Api\Fornecedor\FornecedorController::show() must be an instance of App\Models\Fornecedor, string given
这就是所有的痕迹:
in FornecedorController.php line 48
at FornecedorController->show('1', object(Fornecedor))
at call_user_func_array(array(object(FornecedorController), 'show'), array('tenant' => '1', 'fornecedor' => object(Fornecedor))) in Controller.php line 55
at Controller->callAction('show', array('tenant' => '1', 'fornecedor' => object(Fornecedor))) in ControllerDispatcher.php line 44
at ControllerDispatcher->dispatch(object(Route), object(FornecedorController), 'show') in Route.php line 203
at Route->runController() in Route.php line 160
at Route->run() in Router.php line 559
at Router->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in GetUserFromToken.php line 46
at GetUserFromToken->handle(object(Request), object(Closure)) in
...
我可以验证在检索参数时显然存在混淆。检查路由参数时,结果如下:
public function show($fornecedor)
{
dd(\Route::current()->parameters());
}
网址:/ api / v1 / 1 / fornecedores / 18
结果:
array:2 [
"tenant" => "1"
"fornecedor" => "18"
]
尝试检索提供商的路线时:
public function show($fornecedor)
{
dd($fornecedor);
}
网址:/ api / v1 / 1 / fornecedores / 18
结果:
"1"
即使使用bind我得到的参数也不正确。其他不使用第二个参数的路线也可以。
有人可以告诉我一个更好的方法吗?
谢谢!
答案 0 :(得分:0)
首先猜测是您试图隐式绑定fornecedor
参数,而Laravel无法解析或识别它。
您可以在路线文件(routes/web.php
或routes.php
)中的某处声明参数模型关系
Route::model('fornecedor', \App\Models\ Fornecedor::class);
此外,您的show
方法无法满足tenent
参数的要求。该方法应如下所示:
public function show($tenant, Fornecedor $fornecedor){
return $fornecedor;
}
或
public function show(Tenant $tenant, Fornecedor $fornecedor){
return $fornecedor;
}