Zend Framework 2 passes a variable to a model

I am currently developing a multilingual website. For the multilingual part, I use the / poedit translator. I store the selected language in the session. It works great.

module.php:

public function onBootstrap(MvcEvent $e)
{
    // ...

    $session = new Container('base');

    if ($session->language !== NULL) {
        $e->getApplication()->getServiceManager()->get('translator')->setLocale($session->language);
    }
}

Action for setting the language in the controller:

public function setLanguageAction()
{
    $language = $this->params()->fromRoute('language');

    if (isset($this->languages[$language])) {

        $session = new Container('base');

        if ($language == 'en') {
            $session->language = NULL;
        } else {
            $session->language = $language;
        }
    }

    return $this->redirect()->toRoute('home');
}

In module.config.php, the locale is set to en by default.

Like I said, everything works fine, except for one.

I also store some language-specific data in DB, so in my models I need to know what the current language is. The current language is also needed for other purposes in models.

So, I include the following code in each design function of the model:

$session = new Container('base');

if ($session->language !== NULL) {
    $this->language = $session->language;
} else {
    $this->language = 'default';
}

I think this is not the best solution. I have too many models to always include this code.

, $language , , Module.php/getServiceConfig:

public function getServiceConfig()
{        
    $session = new Container('base');

    return array(
        'factories' => array(
            'Application\Model\SomeThing' => function($sm) {
                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                $c = new SomeThing($dbAdapter);
                $c->language = $session->language;
                return $c;
            }
        )
    );
}

, , - , factory (, , ).

?

!

M

+5
2

, .

. , . , , ( , ) . , invokables , , , . ( ).

, , , . LanguageAwareInterface.

namespace User\Model;

interface LanguageAwareInterface {
    /**
    * Gets the language
    *
    * @return string
    */
    public function getLanguage();

    /**
    * Sets the language
    *
    * @param string $language
    */
    public function setLanguage($language);
}

.

namespace User\Model;

class MyModel implements \User\Model\LanguageAwareInterface {
    /**
    * @var string $language The current language
    */
    protected $language;

    /**
    * Gets the language
    *
    * @return string
    */
    public function getLanguage() {
        return $this->language;
    }

    /**
    * Sets the language
    *
    * @param string $language
    */
    public function setLanguage($language) {
        $this->language = $language;
    }
}

, , , , , , . , , .

. , , , (, Application, Zend). ( ) Module getServiceConfig(), YourModule/config/module.config.php service_manager. ; , - , . .

return array(
    /* Other configuration here */

    'service_manager' => array(
        'initializers' => array(
            'language' => function($service, $sm) {
                // $service is the service that is being fetched from the service manager
                // $sm is the service manager instance
                if ($service instanceof \User\Model\LanguageAwareInterface) {
                    $session = new \Zend\Session\Container('base');

                    if ($session->language === null) {
                        // Fetch default language from configuration
                        $config = $sm->get('Config');

                        $session->language = $config['translator']['locale'];
                    }

                    $service->setLanguage($session->language);
                }
            },
        ),
    ),
);

, , , , . , . , , , . , . , , , , , . , , , , . , , , ; . .

+4

, Module.php , , , , .

public function getServiceConfig()
{        
    return array(
        'factories' => array(
            'AppSession' => function($sm) {
                  $session = new \Path\To\Session();
                  $session->doSomething();
                  return $session;
            }
        )
    );
}

, .

public function getServiceConfig()
{        
    return array(
        'factories' => array(
            'Application\Model\SomeThing' => function($sm) {
                $c = new SomeThing();
                // You will have to create the set and get functions.
                $c->setDbAdapterService($sm->get('Zend\Db\Adapter\Adapter'));
                $c->setSessionService($sm->get('AppSession'));
                return $c;
            }
        )
    );
}

, .

module.config.php...

return array(
    'controllers' => array(
        'factories' => array(
            'Application\Controller\Something' => function ($sm) {
                $locator = $sm->getServiceLocator();
                $c = new Application\Controller\SomethingController();
                $c->setSessionService($locator->get('AppSession'));
                return $c;
            },
        ),
    ),
    'router' => array(
        'routes' => array(
            'myroute' => array(
                'type' => 'Zend\Mvc\Router\Http\Segment',
                'options' => array(
                    'route' => '[/:action]',
                    'defaults' => array(
                        'controller' => 'Application\Controller\Something',
                        'action'     => 'index',
                    ),
                ),
            ),
        ),
    ),
);
0

All Articles