gpt4 book ai didi

php - Lumen中的隐式路由模型绑定(bind)?

转载 作者:行者123 更新时间:2023-12-02 06:31:17 26 4
gpt4 key购买 nike

我正在尝试在 Lumen 中使用隐式路由模型绑定(bind),但似乎它不起作用。

反正有没有启用这个?

$app->get('/users/{user}', 'UsersController@get');

它只是返回 user 中的值但它不是类型提示并返回模型。

最佳答案

我创建了一个包来添加对 route-model-binding 的支持在流明,在这里查看:

Lumen Route Binding

这个需要 :

php >= 7.1
Lumen 5.* || 6.*

它支持显式绑定(bind):
$binder->bind('user', 'App\User');

隐式绑定(bind):
$binder->implicitBind('App\Models');

和复合绑定(bind):(将多个通配符绑定(bind)在一起,在像 posts\{post}\comments\{comment} 这样的情况下,您将能够将其绑定(bind)到可解析 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');

可以在存储库上使用自定义方法:
// Use a custom method on the class
$binder->implicitBind('App\Repositories', '', 'Repository', 'findForRoute');

您可以在存储库类的哪个位置执行以下操作:
public function findForRoute($val)
{
return $this->model->where('slug', $val)->firstOrFail();
}

关于php - Lumen中的隐式路由模型绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35670622/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com