how to call __construct of BasePresenter in SignPresenter ?

6 months ago

thcom
Member | 55
+
0
-

Hi, i am little confused,

in basePresenter i create dataModel for acces to db for all presenters

<?php
	/** @var DataManager */
	public $dataModel;

	public function __construct(DataModel $dataModel)	{
		$this->dataModel = $dataModel;
?>

but now in SingPresenter is my model not working

i found, that Base presenter has own __construct method, and __construct of BasePresenter is not executed

i solve this, i added datamodel creation to BasePresenter __construct

<?php

	/** @var Forms\SignInFormFactory */
	private $signInFactory;

	/** @var Forms\SignUpFormFactory */
	private $signUpFactory;

	/** @var DataManager */
	public $dataModel;

	public function __construct(Forms\SignInFormFactory $signInFactory, Forms\SignUpFormFactory $signUpFactory, DataModel $dataModel)	{
		$this->signInFactory = $signInFactory;
		$this->signUpFactory = $signUpFactory;
		$this->dataModel = $dataModel;

	}

?>

but i am not sure if it is correct

shall i call something like

parent::__construct in SingPresenter ?

thnak you !!

6 months ago

Mabar
Member | 286
+
+3
-

Yup.

	public function __construct(DataModel $dataModel, Forms\SignInFormFactory $signInFactory, Forms\SignUpFormFactory $signUpFactory)	{
		parent::__construct($dataModel);
		$this->signInFactory = $signInFactory;
		$this->signUpFactory = $signUpFactory;
	}

6 months ago

dkorpar
Backer | 93
+
+3
-

in abstract classes like BasePresenter it's easier to use inject method or @inject annotation
https://doc.nette.org/en/3.0/di-usage#…
that way you won't need to pass it in all presenters and call parrent::__construct with params.

You can also use decorator:

decorator:
	App\Presenters\BasePresenter:
		setup:
			- setFoo('bar')

Last edited by dkorpar (2020-01-02 11:45)

6 months ago

thcom
Member | 55
+
0
-

thank you very much, it works perfect !!