我正在使用存储库模式在Laravel中编写应用程序。主要想法是 编写一个存储库,根据路由参数处理2个模型。
所以我的routes.php
看起来像这样:
routes.php文件
<?php $api = app('Dingo\Api\Routing\Router');
$api->version('v1', function ($api)
{
$api->post('create/{type}', ['uses' => 'Beyondi\Account\Http\Controllers\AccountController@insert']);
});
AccountController.php
class AccountController extends Controller
{
/**
* Generate JSON Web Token.
*/
protected $account;
public function __construct(TypeRepositoryInterface $user)
{
$this->account = $user;
}
public function insert(AccountRequest $request)
{
if($this->account->create($request->input()))
{
return $this->response->array("User inserted")->setStatusCode(200);
}else
{
return $this->response->array("Error, user not inserted")->setStatusCode(500);
}
}
AccountRequest.php
class AccountRequest extends Request
{
public function __construct(FormRequest $request)
{
if ($request->route('type')=='consumer')
{
$rules=[
'looking_for' => 'required|in:male,female,trans',
'photo' => 'required',
'newsletter' => 'boolean|required'
];
$this->rules($rules);
}else
{
$rules=[
'type' => 'required|in:private,agency',
'sexuality' => 'required|in:hetero,bi,homo',
'height' => 'required|numeric'
];
$this->rules($rules);
}
}
public function rules($rules)
{
return [$rules];
}
public function authorize()
{
return true;
}
}
TypeRepositoryInterface.php
interface TypeRepositoryInterface
{
public function create(array $data);
public function update($id, array $data);
public function delete($id);
public function getAll(array $columns = ['*']);
public function findById($id, $columns = ['*']);
}
AbstractRepository.php
abstract class AbstractRepository implements AbstractRepositoryInterface
{
protected $model;
/**
* Create new repository interface
*
* @param Model $model
*/
public function __construct(Model $model)
{
$this->model = $model;
}
/**
* Get new instance of model
*
* @param array $attributes
* @return static
*/
public function getNew(array $attributes = [])
{
return $this->model->newInstance($attributes);
}
/**
* Insert new data in database
*
* @param array $data
* @return static
*/
public function create(array $data)
{
return $this->model->create($data);
}
/**
* Update data in database
*
* @param $id
* @param array $data
* @return mixed
*/
public function update($id, array $data)
{
return $this->model->whereId($id)->update($data);
}
/**
* Delete data from database
*
* @param $id
* @return bool|null
* @throws \Exception
*/
public function delete($id)
{
return $this->model->delete($id);
}
/**
* Get all data from database
*
* @param array $columns
* @return \Illuminate\Database\Eloquent\Collection|static[]
*/
public function getAll(array $columns = ['*'])
{
return $this->model->all($columns);
}
/**
* Find data by id
*
* @param $id
* @param array $columns
* @return \Illuminate\Support\Collection|null|static
*/
public function findById($id, $columns = ['*'])
{
return $this->model->find($id, $columns);
}
}
TypeRepository.php
class TypeRepository extends AbstractRepository implements TypeRepositoryInterface
{
protected $model;
/**
* Create new repository interface
*
* @param Model $model
*/
public function __construct(ProviderModel $providerModel,ConsumerModel $consumerModel,Request $request, $type)
{
if ($request->route('type')=='consumer')
{
$this->model = $consumerModel;
}else
{
$this->model = $providerModel;
}
}
}
正如您所看到的,我正在尝试根据路由TypeRepository
参数在我的{type}
中切换两个模型。但是我得到了
Unresolvable dependency resolving [Parameter #3 [ <required> $type ]] in class Account\\Repositories\\Type\\Eloquent\\TypeRepository
错误。我错过了什么?如何正确地做到这一点?
答案 0 :(得分:1)
您没有对TypeRepository构造函数的$ type参数进行类型提示,因此服务容器无法实例化它。
我建议删除$ type参数并改为使用它:
$type = $request->input('type');
在构造函数中。