2014-04-06 77 views
0

我以非常接近Chris Fidao在​​3210书上使用的方式使用存储库模式。基本上我有具体的存储库类实现其接口,并获得注入模型。将存储库绑定到路由

现在我想利用Laravel的路由绑定。由于我使用的是存储库,因此我无法直接将它们绑定到模型上......对吗?但是我没有这样做。

我使用一个服务提供商绑定我的具体资料库,以接口,这样的:

$app->bind('App\Repositories\UserInterface', function ($app) { 
     return new EloquentUser(new User); 
    }); 

如何将我的路由上绑定库接口?似乎是微不足道的,但我有点失落...

谢谢你提前! :)

+0

为什么要将路由绑定到回购?路由应该指向一个请求处理程序(或多或少的一个服务)而不是一个其目的是将业务与持久性分离的存储库 – MikeSW

+0

您是否找到了解决方案? – Victor

回答

1

你可以用不同的方法来传递模型的形式没有模型结合的路线,例如,假设你有使用UserController的路线,这是控制器:

class UserController extends BaseController { 
    public function __construct(UserInterface $UserRepo) 
    { 
     $this->repo = $UserRepo; 
    } 

    public function edit($id) 
    { 
     $user = $this->user->find($id); 
     return View::make('user.edit')->with('user', $user); 
    } 
} 

您在user.edit视图形式:

{{ Form::model($user, array('route' => array('user.update', $user->id))) }} 
0

您可以使用您的路线回购方式如下。但是,你真的需要这个吗?

\\the repo is in the Branches folder 
use App\Repos\Branches\BranchRepository; 

Route::get('branches',function(BranchRepository $branchRepo){ 
    \\using a method of your repo - in this case using getData() method 
    return $branchRepo->getData(); 
});