how to call __construct of BasePresenter in SignPresenter?

thcom
Backer | 94
+
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 !!

Marek Bartoš
Nette Blogger | 1259
+
+3
-

Yup.

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

dkorpar
Member | 135
+
+3
-

in abstract classes like BasePresenter it's easier to use inject method or @inject annotation
https://doc.nette.org/…dependencies#…
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)

thcom
Backer | 94
+
0
-

thank you very much, it works perfect !!