Access presenter property from neon

Notice: This thread is very old.

4 years ago

Bejn
Member | 17
+
0
-

Hello,

I have service, that needs parameter from presenter or route. My router assigns value to variable BasePresenter::$locale. I need to use this value in a service. Is it possible in neon?

namespace App\AdminModule\Presenters;

use Nette;
use App\Model,
	App\Model\Objects\Lang;


/**
 * Base presenter for all application presenters.
 */
abstract class BasePresenter extends Nette\Application\UI\Presenter
{
 	/** @persistent */
    public $locale;

    /** \App\Model\Objects\Lang */
    protected $lang;

    /** @var \Kdyby\Translation\Translator @inject */
    public $translator;

	/** @var \App\Model\Objects\PageManager @inject */
	public $pageManager;

	function startup() {
		parent::startup();
		$this->lang = new Lang($this->locale);
		$this->pageManager->setLang($this->lang);
	}
}

Can I write in neon something like:

services:
	- App\Model\Objects\Lang(application.presenter.locale)

4 years ago

Bejn
Member | 17
+
0
-

OK, I solved it by passing whole application.

services:
    - App\Model\Objects\Lang(@application)

4 years ago

Bejn
Member | 17
+
0
-

OK, I figured it out:

	lang: App\Model\Objects\Lang(@application::getPresenter()::getLocale())

4 years ago

Bejn
Member | 17
+
0
-

i found out, that geting that param from presenter is too late. So I changed it

services:
	lang: App\Model\Objects\Lang(App\Model\Utils::getAppRouterParam(@application, 'locale'))
<?php
namespace App\Model;

use Nette,
	Nette\Application\Application;

class Utils {
	public static function getAppRouterParam(Application $app, $param) {
		return $app->getRequests()[0]->getParameter($param);
	}
}