作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 Lumen 中使用隐式路由模型绑定(bind),但似乎它不起作用。
反正有没有启用这个?
$app->get('/users/{user}', 'UsersController@get');
user
中的值但它不是类型提示并返回模型。
最佳答案
我创建了一个包来添加对 route-model-binding
的支持在流明,在这里查看:
Lumen Route Binding
这个需要 :
php >= 7.1
Lumen 5.* || 6.*
$binder->bind('user', 'App\User');
$binder->implicitBind('App\Models');
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/
我是一名优秀的程序员,十分优秀!