在laravel-livewire / project中,我需要使用Repositories。我找到了https://github.com/andersao/l5-repository库,并查看了如何将Repository分配给PostsController的示例
namespace App\Http\Controllers;
use App\PostRepository;
class PostsController extends BaseController {
protected $repository;
public function __construct(PostRepository $repository){
$this->repository = $repository;
}
....
}
我试着做类似的组件:
use App\Repositories\Interfaces\FacilityRepositoryInterface;
class Facilities extends Component
{
use WithPagination;
public $form= [
'name'=>'',
'descr'=> '',
'created_at'=> '',
'is_reopen' => false,
];
public $current_facility_id;
public $filter_name= '';
public $updateMode = 'browse';
protected $FacilityRepository;
public function __construct(FacilityRepositoryInterface $FacilityRepository)
{
$this->FacilityRepository = $FacilityRepository;
}
public function render()
{
$backend_per_page = Settings::getValue('backend_per_page', CheckValueType::cvtInteger, 20);
$this->emit('facility_opened', [ 'mode'=>'browse', 'id'=>null ] );
return view('livewire.admin.facilities.container', [
'facilityDataRows' => $this->FacilityRepository->filterWithPagination(
[
'name'=>$this->filter_name,
'per_page'=> $backend_per_page
]
),
'facility_rows_count'=> $this->facility_rows_count
]);
}
其中filterWithPagination是
class FacilityRepository extends BaseRepository implements FacilityRepositoryInterface
{
private $UserRepository;
...
I found definition of __construct in in vendor/livewire/livewire/src/Component.php as :
public function __construct($id)
{
$this->id = $id;
$this->ensureIdPropertyIsntOverridden();
$this->initializeTraits();
}
有没有有效的方法?
谢谢!
4条答案
按热度按时间pdsfdshx1#
我也有同样的担心。我认为livewire目前不支持依赖注入。你现在可以做的是将你的仓库注入到mount方法中。
0tdrvxhp2#
应该使用
mount
方法进行依赖注入所以这个应该能用
参见文档:https://laravel-livewire.com/docs/rendering-components#injecting-parameters
wfsdck303#
如果你的动作需要任何服务,这些服务应该通过Laravel的依赖注入容器来解决,你可以在动作的签名中将它们列在任何附加参数之前
https://laravel-livewire.com/docs/2.x/actions
eulz3vhy4#
在你的livewire组件中使用这个