将存储库绑定到路由

时间:2014-04-06 05:54:20

标签: binding laravel repository-pattern laravel-routing

我正在使用Repository模式,其方式与Chris Fidao在Implementing Laravel书上使用的方式非常接近。基本上我有具体的存储库类来实现其接口并注入模型。

现在我想利用Laravel的路线绑定。由于我使用的是存储库,我不能直接将它们绑定到模型上......对吧?但是我没有做到这一点。

我正在使用服务提供程序将我的具体存储库绑定到接口,例如:

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

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

提前谢谢! :)

2 个答案:

答案 0 :(得分: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))) }}

答案 1 :(得分: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();
});