Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8733089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:34:29+00:00 2026-06-13T09:34:29+00:00

My question in short: Can I use a single Factory for multiple Controllers? More

  • 0

My question in short: Can I use a single Factory for multiple Controllers?

More details:

I have some global non-module specific settings in /config/autoload/global.php which look like this:

return array(
    'settings' => array(
        'setting_a' => 'foo',
        'setting_b' => 'bar'
    ),

    // More ZF default configuration ...
);

Now I want to make these settings accessable in every controller without having to call $this->getServiceLocator()->get('config') all the time.

So my idea was to introduce a class attribute $settings in my AbstractController which get’s injected with the configuration array. I tried to fetch the config directly in the constructor of AbstractController. However getServiceLocator() doesn’t seem ready at the time and returns NULL.

I could build Controller Factories for every controller and inject the settings like this:

class ControllerFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator) {
        $config = $serviceLocator->get('config');
        return new \MyModule\Controller\MyController($config['settings']);
    }
}

But it would be the same over and over again. So my question is: Can I use a single Factory for multiple controllers?

In my module.config.php I could specify a single factory class for multiple controllers:

return array(
    'controllers' => array(
        'factories' => array(
            'MyModule\Controller\MyControllerA' => 'MyModule\Factory\ControllerFactory',
            'MyModule\Controller\MyControllerB' => 'MyModule\Factory\ControllerFactory',
            'MyModule\Controller\MyControllerC' => 'MyModule\Factory\ControllerFactory',
        )
    ),
);

But in the factory I need to return the actual Controller object by hand (see example above) which of course only works with one Factory per Controller.

Hope, I made my problem clear.

UPDATE 2013-03-24:

Although I first followed the suggested solution by creating an initializer, I never really liked using it just for the injection of the configuration.

So I kept digging around and ended up creating a Controller plugin to receive the settings.

The code for the plugin looks like this:

use Zend\Mvc\Controller\Plugin\AbstractPlugin;

class Settings extends AbstractPlugin
{
    protected $settings;

    public function __invoke()
    {
        $config = $this->getController()->getServiceLocator()->get('Config');

        if (isset($config['settings']) && is_array($config['settings'])) {
            $this->settings = $config['settings'];
        }

        return $this->settings;
    }
}

After adding the plugin in module.config.php with

'controller_plugins' => array(
    'invokables' => array(
        'settings' => 'My\Controller\Plugin\Settings',
    )
),

I can easily access my settings within a controller by simply calling $this->settings(). Hope this helps anyone.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:34:30+00:00Added an answer on June 13, 2026 at 9:34 am

    You could try attaching an initializer, then as the controller is created the instance is checked against known initializers. If the instance matches a given interface or abstract class, then you can apply some common logic.

    I’ve not tested this approach with controllers but given that the ControllerLoader is a type of ServiceManager/ServiceLocator it should in theory work.

    'controllers' => array (
        'initializers' => array(
            'MyApplication\Controller\Initializer'
        )
    ),
    

    Then the Initalizer would look something like:

    class Initializer implements InitializerInterface
    {
        public function initialize($instance, ServiceLocatorInterface $serviceLocator)
        {
            if (!$instance instanceof MyControllerInterface)
            {
                return;
            }
    
            // Do something for this instance
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A short question: I've got a TabNavigator with multiple canvas children. How can i
Short question is: how can i use graph api oauth redirects mechanism to authenticate
Short question: If I have class that impelemnts FactoryBean interface, how can I get
Short Question: Can I specify wildcards for custom console folding? If so, what is
Short question: Can anyone tell me what the requirements (especially when it comes to
I've got a short question. How can I update a TextField without using an
This question will be short and sweet. I know an instruction can occur between
The short version of the question - why can't I do this? I'm restricted
SHORT VERSION OF QUESTION: So basically my question is: How can I set the
Short and simple question. In Winforms you can bind a dataview to a combobox

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.