如何在laravel-livewire项目中添加l5仓库?

j8ag8udp  于 2023-04-22  发布在  其他
关注(0)|答案(4)|浏览(121)

在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();
    }

有没有有效的方法?
谢谢!

pdsfdshx

pdsfdshx1#

我也有同样的担心。我认为livewire目前不支持依赖注入。你现在可以做的是将你的仓库注入到mount方法中。

public function mount(FacilityRepositoryInterface $FacilityRepository)
    {
        $this->FacilityRepository = $FacilityRepository;
    }
0tdrvxhp

0tdrvxhp2#

应该使用mount方法进行依赖注入
所以这个应该能用

class Facilities extends Component {
  public function mount(FacilityRepositoryInterface $FacilityRepository) {
    $this->FacilityRepository = $FacilityRepository;
  }
}

参见文档:https://laravel-livewire.com/docs/rendering-components#injecting-parameters

wfsdck30

wfsdck303#

如果你的动作需要任何服务,这些服务应该通过Laravel的依赖注入容器来解决,你可以在动作的签名中将它们列在任何附加参数之前

public function addTodo(TodoService $todoService, $id, $name)
    {
        ...
    }

https://laravel-livewire.com/docs/2.x/actions

eulz3vhy

eulz3vhy4#

在你的livewire组件中使用这个

public function hydrate()
    {
        $this->setRepository();
    }

    public function setRepository()
    {
        $this->cityRepository = App::make(CityRepository::class);
        $this->districtRepository = App::make(DistrictRepository::class);
        $this->villageRepository = App::make(VillageRepository::class);
    }

相关问题