How to create own application configuration?
- zalizko
- Member | 7
Hello everyone!
I'm starting with nette and confusing, I can't realize the elementary
thing – own config.
For example I wold like to setup available languages in application.
I have added to the end of config.neon
- languages
- en
- ru
and would like to get somehow this array in the BaseController.
Now my application uses this config throw error:
“Found sections ‘languages’ in configuration, but corresponding extensions
are missing.”
Could anybody help me?
- zalizko
- Member | 7
mm… ok! thanks for reply!
but now how to get this config from controller?
<?php
abstract class BasePresenter extends Nette\Application\UI\Presenter {
protected function beforeRender() {
var_dump($this->context->getParameters()['languages']);
}
?>
right? Finally I have found only this way…
- Michal Vyšinský
- Member | 608
Clean way is to create a service, which will hold languages. Something like ‘LanguagesStorage’.
Define service:
services:
- LanguagesStorage(%languages%)
Service class could look like this:
class LanguagesStorage extends Nette\Object
{
private $languages = array();
public function __construct(array $languages)
{
$this->languages = $languages;
}
public function getLanguages()
{
return $this->languages;
}
}
Usage:
class AnyPresenter extends Presenter
{
/** @var LanguagesStorage @inject */
public $languagesStorage;
public function renderDefault()
{
dump($this->languagesStorage->languages);
}
}
And you can inject your service everywhere (in presenter via @inject, in other service via constructor).
Last edited by Michal Vyšinský (2014-06-19 15:29)
- zalizko
- Member | 7
Uhhh.. thanks! Looks clear now. Service for holding few parameters..
overhead..
Is it here some another way? these parameters in the config.neon, thay saves in
the SystemConfigurer? How can I get access to him / her properties? All
presenters are nested from BasePresenter then the main logic can be inside
BasePresenter (or?).
offtop: one thing related to multilingual, how i can get current language? change it? of couse I can do it by sessio or other things, is there some functionallity out-of-the-box?