Passing Settings to Presenters problem

alnux
Member | 139
+
0
-

hi i follow the doc of passing settings to presenter.

I have a BasePresenter for my presenters but it is located in a different place than all the presenters, here my neon mapping

application:
	errorPresenter: Error
	mapping:
		*: [App\Modules, *, Presenters\*Presenter]

and my BasePresenter namespace is App\Common\Presenters but all other presenter are in respective module by example App\Modules\Admin\User\Presenters

the thing is that the passing settings does not work with constructor becouse with bdump show like a unset variable but works with inject properties

here my Settings class

namespace App\Common\Classes;

namespace App\Common\Classes;

class SettingsCaller
{
	public function __construct(
		public readonly bool $debugMode,
		public readonly string $appDir,
        public readonly array $settings,
	) {}
}

here my BasePresenter

namespace App\Common\Presenters;

use Nette;
use Nette\Application\UI\Template;
use Nette\Application\UI\Presenter;
use Nette\DI\Attributes\Inject;

class BasePresenter extends Presenter
{
	#[Inject]
	public \Alnux\LiveTranslator\Translator $translator;


	function __construct(protected \App\Common\Classes\SettingsCaller $setup) {
	}
}

my neon file

services:
	# Ruter Factory
	- App\Router\RouterFactory::createRouter

	# Model Modules Factories
	- App\Modules\Admin\User\Model\UserModelFactory

	# Forms Modules Factories
	- App\Modules\Admin\User\Form\UserFormFactory

	translatorStorage: Alnux\LiveTranslator\Storage\File(%appDir%/common/languages/app)
	translator:
		# English will be allways language for the translator class. This becouse source files are on english
		class: Alnux\LiveTranslator\Translator(en)
		setup:
			- setAvailableLanguages(%settings.location.enabled_languages%)
	translatorPanel: Alnux\LiveTranslator\Panel

	- App\Common\Classes\SettingsCaller(
		%debugMode%,
		%appDir%,
		%settings%,
	)

the dump show that it is unset screenshot

but if i put with inject like translator var it works

Rick Strafy
Nette Blogger | 81
+
+1
-

Hi, about that presenter directory structure, take look at https://github.com/…Module/Front, it's much better and solves that problem so you can have it in one directory and subdirectories.

So why dont you use #[Inject]? I use only #[Inject] attribute in my base presenters. The problem is, you need to call __parent() constructor in all your presenters extending this base, so that why inject attribute is better option, and probably the reason for unset.

Last edited by Rick Strafy (2023-08-21 20:23)

Marek Bartoš
Nette Blogger | 1261
+
+2
-

Likely forgot to call parent method. Also UI\Presenter has a constructor that you should call when overriding it.

class A {
    function example() {
        echo "I am A::example() and provide basic functionality.<br />\n";
    }
}

class B extends A {
    function example() {
        echo "I am B::example() and provide additional functionality.<br />\n";
        parent::example();
    }
}
alnux
Member | 139
+
0
-

Ups, so so so so sorry. where i have my head??, you are right, sorry about your time.

alnux
Member | 139
+
0
-

Rick Strafy wrote:

Hi, about that presenter directory structure, take look at https://github.com/…Module/Front, it's much better and solves that problem so you can have it in one directory and subdirectories.

So why dont you use #[Inject]? I use only #[Inject] attribute in my base presenters. The problem is, you need to call __parent() constructor in all your presenters extending this base, so that why inject attribute is better option, and probably the reason for unset.

because I think the setting data is important to pass it through inject. not for something @DavidGrudl made a separate section for passing app settings.

becouse here says
On the other hand, this method suffers from the same shortcomings as passing dependencies into properties in general: we have no control over changes in the variable, and at the same time, the variable becomes part of the public interface of the class, which is undesirable.

Im a good way? or it doesnt matter this???

by the way @RickStrafy thanks for open my eyes about parent constructor i I totally forgot. :-)

Rick Strafy
Nette Blogger | 81
+
0
-

Sorry @alnux, I came to the forum only occassilnally when I got notified at nette discord server, I think that injectSomething() is much worse than #[Inject] public Service $service, I just don't see a difference, it's not like your presenter is some public api for other developers/companies and someone can touch it and change your variable.

Last edited by Rick Strafy (2023-08-27 17:57)