简体   繁体   English

流明中的隐式路由模型绑定?

[英]Implicit route model binding in Lumen?

I'm trying to use implicit route model binding in Lumen but seems it's not working. 我正在尝试在Lumen中使用隐式路由模型绑定,但似乎不起作用。

Is there anyway to enable this? 反正有启用此功能的功能吗?

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

It's just returning the value in user but it's not type hinting it and returning the model. 它只是在user返回值,而不是在类型上暗示它并返回模型。

I created a package to add support for route-model-binding in Lumen, Check it out here : 我创建了一个包来添加对Lumen中的route-model-binding支持,请在此处查看:

Lumen Route Binding 流明路线绑定

It requires : 这个需要 :

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

It supports Explicit binding : 它支持显式绑定:

$binder->bind('user', 'App\User');

Implicit binding : 隐式绑定:

$binder->implicitBind('App\Models');

And Composite binding : (bind more than one wildcard together, In situations like posts\\{post}\\comments\\{comment} you will be able to bind it to a callable that will resolve the Post and the Comment related to the post) 和Composite绑定:(将多个通配符绑定在一起,在诸如posts\\{post}\\comments\\{comment}您将能够将其绑定到可解决该Post和与该Post相关的Comment的可调用对象)

$binder->compositeBind(['post', 'comment'], function($postKey, $commentKey) {
    $post = \App\Post::findOrFail($postKey);
    $comment = $post->comments()->findOrFail($commentKey);

    return [$post, $comment];
});

Also can work with other classes like Repositories : 也可以与其他类(例如Repositories

// Add a suffix to the class name
$binder->implicitBind('App\Repositories', '', 'Repository');

Can use a custom method on the repository : 可以在存储库上使用自定义方法:

// Use a custom method on the class
$binder->implicitBind('App\Repositories', '', 'Repository', 'findForRoute');

Where in the repository class you can do : 您可以在存储库类中的哪个位置执行操作:

public function findForRoute($val)
{
    return $this->model->where('slug', $val)->firstOrFail();
}

I ran into the same issue recently and doubt that it's possible. 我最近遇到了同一问题,并且怀疑是否有可能。 Lumen 5.2 does not use the Illuminate router but FastRoute instead. 流明5.2不使用Illuminate路由器,而是使用FastRoute more info on the differences here However, it should be possible to write a custom middleware if that's an option. 有关此处差异的更多信息,但是,如果可以的话,应该可以编写自定义中间件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM