Search code examples
phpzend-frameworkzend-framework3zf3

Access local parameters from module services and controllers


I´m developing a site based on Zend Framework 3 and in some modules I need to send e-mails.

I´m using PERL Mail to do so. It willwill foward all e-mail send requests to Amazon SES service on production, and for development I´m using a free gmail account.

In my application I want to store the mail configuration in a local way using the local.php file at that project/config/autoload directory. In that way I can have different configurations for both development and production. So, I´ve created the following entries on my local.php file:

'mail' => [
   'host' => 'ssl://smtp.gmail.com',
   'port' => '465',
   'auth' => true,
   'username' => '[email protected]',
   'password' => 'mypassword',
]

All fine, except that I don´t know how to get these parameters from my modules services and controllers.

Here is a service example that I need to access this parameter, located at module/User/src/service/UserManagerService:

class UserManager
{
    /**
     * Doctrine entity manager.
     * @var Doctrine\ORM\EntityManager
     */
    private $entityManager;  

    public function __construct($entityManager) 
    {
        $this->entityManager = $entityManager;
    }


    public function addUser($data) 
    {
        **// Need to access the configuration data from here to send email**
    }
}  

This service has a factory:

<?php
namespace User\Service\Factory;

use Interop\Container\ContainerInterface;
use User\Service\UserManager;

class UserManagerFactory
{
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {        
        $entityManager = $container->get('doctrine.entitymanager.orm_default');

        return new UserManager($entityManager);
    }
}

I´m pretty new to these ZF3 factories, services and controllers, so I´m little lost here.

How can I get the parameters stored in the local.php file here at this service ? Would that approach be the same for a controller ?


Solution

  • The configuration is stored in the container under Config. You can retrieve it like you get a service or anything else from the container. This works for anything (services, controllers) you retrieved from the container with a factory. Best practice is to retrieve configuration options inside the factory and pass it to the service or controller.

    Your factory:

    <?php
    
    namespace User\Service\Factory;
    
    use Interop\Container\ContainerInterface;
    use User\Service\UserManager;
    
    class UserManagerFactory
    {
        public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
        {
            $entityManager = $container->get('doctrine.entitymanager.orm_default');
            $config = $container->get('Config');
    
            return new UserManager($entityManager, $config['mail']);
    
            /* Or write this as:
            return new UserManager(
                $container->get('doctrine.entitymanager.orm_default'), 
                $container->get('Config')['mail']
            );
            */
        }
    }
    

    Your Service:

    <?php
    
    use Doctrine\ORM\EntityManager
    
    class UserManager
    {
        /**
         * @var EntityManager
         */
        private $entityManager;
    
        /**
         * @var array
         */
        private $mailConfig;
    
        public function __construct(EntityManager $entityManager, array $mailConfig)
        {
            $this->entityManager = $entityManager;
            $this->mailConfig = $mailConfig;
        }
    
        public function addUser($data)
        {
            var_dump($this->mailConfig);
        }
    }