2016-02-27 56 views

回答

1

我最近碰到同樣的問題,懷疑它是可能的。 Lumen 5.2不使用Illuminate路由器,而是使用FastRoutemore info on the differences here 但是,如果這是一個選項,應該可以編寫自定義中間件。

+0

決定拋售流明的Laravel,只是太多的頭痛。 – Rob

+0

也許你可以試試這個軟件包https://packagist.org/packages/mmghv/lumen-route-binding, –

2

我創建了一個包流明增加對route-model-binding,在這裏檢查它:

Lumen Route 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(); 
}