【发布时间】:2018-08-21 15:16:06
【问题描述】:
我已经关注了很多 Laravel 仓库的教程和类似教程的 msot。下面的链接之一
https://itsolutionstuff.com/post/laravel-5-repository-pattern-tutorial-from-scratchexample.html
第 1 步:创建界面
在第一步中,我们必须创建接口,然后在 app 文件夹中创建 Repositories(app/Repositories) 目录。我们还需要在 Repositories 文件夹中创建 User(app/Repositories/User) 文件夹。
好的,现在首先我们将在用户目录中创建 UserInterface,所以首先创建 UserInterface.php 文件并将下面的代码放入该文件中:
app/Repositories/User/UserInterface.php
namespace App\Repositories\User;
interface UserInterface {
public function getAll();
public function find($id);
public function delete($id);
}
第 2 步:创建存储库
好的,在这一步我们将创建UserRepository.php 用于写入数据库登录,在这个文件中我们将编写我们的数据库登录代码。所以,首先在用户目录中创建UserRepository.php 文件并放入下面的代码。
app/Repositories/User/UserRepository.php
namespace App\Repositories\User;
use App\Repositories\User\UserInterface as UserInterface;
use App\User;
class UserRepository implements UserInterface
{
public $user;
function __construct(User $user) {
$this->user = $user;
}
public function getAll()
{
return $this->user->getAll();
}
public function find($id)
{
return $this->user->findUser($id);
}
public function delete($id)
{
return $this->user->deleteUser($id);
}
}
第 3 步:创建服务提供者
在这一步中,我们必须为绑定UserInterface 和UserRepository 类创建服务提供者。所以让我们在用户文件夹中创建UserRepoServiceProvide.php`文件并输入以下代码:
app/Repositories/User/UserRepoServiceProvide.php
namespace App\Repositories\User;
use Illuminate\Support\ServiceProvider;
class UserRepoServiceProvide extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->bind('App\Repositories\User\UserInterface', 'App\Repositories\User\UserRepository');
}
}
现在,我们必须在 app.php 文件中添加服务器提供,所以在 config/app.php 中添加 UserRepoServiceProvide 并添加下面一行。
配置/app.php
return [
....
'providers' => [
......,
App\Repositories\User\UserRepoServiceProvide::class,
]
....
]
第 4 步:创建用户模型
我认为我们已经有了用户模型,所以你只需要添加 getAll()、findUser() 和 deleteUser() 函数。但是你也可以通过下面的代码。所以,让我们在你的模型上添加下面的代码。
app/User.php
命名空间应用程序;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function getAll()
{
return static::all();
}
public function findUser($id)
{
return static::find($id);
}
public function deleteUser($id)
{
return static::find($id)->delete();
}
}
第 5 步:在控制器中使用
现在我们准备在控制器中使用我们的存储库模式。因此,让我们在 UserController.php 文件中添加以下代码。
app/Http/Controllers/UserController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Repositories\User\UserInterface as UserInterface;
class UserController extends Controller
{
public function __construct(UserInterface $user)
{
$this->user = $user;
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$users = $this->user->getAll();
return view('users.index',['users']);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$user = $this->user->find($id);
return view('users.show',['user']);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function delete($id)
{
$this->user->delete($id);
return redirect()->route('users');
}
}
现在最后只需触发下面的命令,因为我们必须自动加载类。
作曲家更新
php artisan optimize
我的问题是大多数教程在 UserRepository 中注入了用户模型,所以如果这样做我需要为所有模型分别创建存储库。有什么方法可以让我使用单个存储库并注入模型
【问题讨论】:
标签: php laravel laravel-5 repository-pattern