Skip to content

Instantly share code, notes, and snippets.

@settermjd
Created April 16, 2014 13:58
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save settermjd/10879937 to your computer and use it in GitHub Desktop.
Save settermjd/10879937 to your computer and use it in GitHub Desktop.
// IndexControllerFactory.php
namespace YourModule\Controller\Factory;
use Zend\ServiceManager\FactoryInterface,
Zend\ServiceManager\ServiceLocatorInterface,
Zend\ServiceManager\Exception\ServiceNotCreatedException,
YourModule\Controller\IndexController;
class IndexControllerFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
$sm = $serviceLocator->getServiceLocator();
try {
$cache = $sm->get('Application\Cache');
} catch (ServiceNotCreatedException $e) {
$cache = null;
} catch (ExtensionNotLoadedException $e) {
$cache = null;
}
$controller = new IndexController($cache);
return $controller;
}
}
// IndexController.php
use Zend\Cache\Storage\Adapter\AbstractAdapter;
class IndexController
{
protected $_cache;
public function __construct(AbstractAdapter $cache = null)
{
if (!is_null($cache)) {
$this->_cache = $cache;
}
}
}
// module.config.php
return array(
'controllers' => array(
'factories' => array(
'YourModule\Controller\Index' => 'YourModule\Controller\Factory\IndexControllerFactory',
)
),
@Baft
Copy link

Baft commented Jun 11, 2016

how i can pass parameters to service on call time . for example to form elements added to form factory :

module.php 
public function getFormElementConfig() {
        return [
             'invokables'=>[
                'Application\Form\UserList'=>'Application\Form\UserListForm',
                     ]];
    }

in form element

UserListForm.php 

class UserListForm extends Fieldset implements InputFilterProviderInterface , ServiceLocatorAwareInterface{

    protected $serviceLocator;

    public function init(){
        $userList=$this->getServiceLocator()->getServiceLocator()->get("Application\Service\Users")->listUsers();

        ... do things on select form element and other elements ...

        $this->get('users')->setValueOptions($usersList);

    }

    public function __construct() {
        parent::__construct ( '' );

        $users=new Select('users');
        $users->setAttributes([ ... ]);
        $users->setOptions([ ... ]);

        $this->add ($users);
    }

    public function getInputFilterSpecification() {
        return array ( ... );
    }

    // .... service locator's methods .....
}

now problem is :

usecase :

//userlist have to show users that are in group 'A'  not all users .  ( A.phtml need dropDown contain user of 'A' group , B.phtml need users of 'B' group )
//how can pass filters (parameters) to userList form element to be set in  " application\Service\Users ->listUsers "  to fetch specific users for "select" element

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment