Laravel 5.6 通过参数id进行依赖注入
Laravel 5.6 Dependency injection through parameter id
我有一条路线:
Route::group(['prefix' => 'admin', 'middleware' => 'auth', 'as' => 'admin.'], function () {
Route::resource('photos', 'Admin\PhotoController', [
'parameters' => [
'photos' => 'alias'
]
]);
});
命令phpartisanroute:list:
使用命令:
php artisan make:controller Admin/PhotoController --resource --model=Models/Photo
我创建了资源控制器Admin/PhotoController,它有一个方法
public function edit(Photo $photo)
{
dd($photo);
}
在数据库 table photos
中,我有列 alias
:
为什么我经过url-addressadmin/photos/alias1/editlaravel 5.6没有自动加载其中alias = alias1的模型照片,而是加载空模型:
如果在地址行而不是参数alias写id admin/photos/1/edit +在路由中,删除参数namealias
,则正常加载Photo模型:
但对我来说参数id不合适,我需要使用别名请告诉我可能是什么问题,是否可以这样做?
默认使用ID。这称为隐式路由模型绑定。您需要使用显式路由模型绑定并注册 alias1 以在服务提供者中使用。在这里查看:https://laravel.com/docs/5.6/routing#explicit-binding
Customizing The Resolution Logic 是您想要的部分。
我有一条路线:
Route::group(['prefix' => 'admin', 'middleware' => 'auth', 'as' => 'admin.'], function () {
Route::resource('photos', 'Admin\PhotoController', [
'parameters' => [
'photos' => 'alias'
]
]);
});
命令phpartisanroute:list:
使用命令:
php artisan make:controller Admin/PhotoController --resource --model=Models/Photo
我创建了资源控制器Admin/PhotoController,它有一个方法
public function edit(Photo $photo)
{
dd($photo);
}
在数据库 table photos
中,我有列 alias
:
为什么我经过url-addressadmin/photos/alias1/editlaravel 5.6没有自动加载其中alias = alias1的模型照片,而是加载空模型:
如果在地址行而不是参数alias写id admin/photos/1/edit +在路由中,删除参数namealias
,则正常加载Photo模型:
但对我来说参数id不合适,我需要使用别名请告诉我可能是什么问题,是否可以这样做?
默认使用ID。这称为隐式路由模型绑定。您需要使用显式路由模型绑定并注册 alias1 以在服务提供者中使用。在这里查看:https://laravel.com/docs/5.6/routing#explicit-binding
Customizing The Resolution Logic 是您想要的部分。