我想在流明中使用隱式路由模型綁定,但似乎它不工作。流明中的隱式路由模型綁定?
有沒有辦法啓用它?
$app->get('/users/{user}', '[email protected]');
它只是返回在user
值,但它不會打字暗示它和返回模型。
我想在流明中使用隱式路由模型綁定,但似乎它不工作。流明中的隱式路由模型綁定?
有沒有辦法啓用它?
$app->get('/users/{user}', '[email protected]');
它只是返回在user
值,但它不會打字暗示它和返回模型。
我最近碰到同樣的問題,懷疑它是可能的。 Lumen 5.2不使用Illuminate路由器,而是使用FastRoute。 more info on the differences here 但是,如果這是一個選項,應該可以編寫自定義中間件。
我創建了一個包流明增加對route-model-binding
,在這裏檢查它:
它要求:
php >= 5.4.0
Lumen 5.*
它支持顯式綁定:
$binder->bind('user', 'App\User');
和隱式綁定:
$binder->implicitBind('App\Models');
和複合結合:(綁定多個通配符一起,在像posts\{post}\comments\{comment}
情況下,你就可以將其綁定到一個可調用,將解決Post
和相關崗位Comment
)
$binder->compositeBind(['post', 'comment'], function($postKey, $commentKey) {
$post = \App\Post::findOrFail($postKey);
$comment = $post->comments()->findOrFail($commentKey);
return [$post, $comment];
});
還可以與其他類的工作就像Repositories
:
// Add a suffix to the class name
$binder->implicitBind('App\Repositories', '', 'Repository');
可以使用自定義實現方法具d對信息庫:
// Use a custom method on the class
$binder->implicitBind('App\Repositories', '', 'Repository', 'findForRoute');
凡在庫類,你可以這樣做:
public function findForRoute($val)
{
return $this->model->where('slug', $val)->firstOrFail();
}
決定拋售流明的Laravel,只是太多的頭痛。 – Rob
也許你可以試試這個軟件包https://packagist.org/packages/mmghv/lumen-route-binding, –