3

I have a model class which does not extend any core Zend module . This model was imported from my previous Zend framework 1 application . I am able to call its methods by converting it to namespace . The problem what I have is in reading global configuration in side the methods defined .

In case of controller I was able to access global configuration using below code

 $config = $this->getServiceLocator()->get('config'); 

// This gives a union of global configuration along with module configuration .

But what should we do to access configuration in side a model class . Below is how my model class is

<?php
namespace test\Http; 

class Request
{

    protected $client;

    public function abc( $c)
    {
        return $something;
    } 


    ......

} 

I am new to Zend framework 2 please kindly suggest any method to achieve this .

In the above description model means ( MVC model class ) which has some business logic in it .

akond
  • 15,865
  • 4
  • 35
  • 55
Aravind.HU
  • 9,194
  • 5
  • 38
  • 50
  • What exactly do you mean by "model" here? Entity? You probably shouldn't do that if that's the question. Consider that you can reach the global config wherever you have a reference to the service locator... – Ocramius Feb 12 '13 at 17:25
  • @Ocramius Thanks for responding to this , I have a model class ( just a class which has business logic ) – Aravind.HU Feb 12 '13 at 17:27
  • So is it a service? Or just an entity containing data + some logic? – Ocramius Feb 12 '13 at 17:29
  • @Ocramius , Just an entity , I dont know what do you mean by entity , But As per my understanding Its an independent business class – Aravind.HU Feb 12 '13 at 17:31
  • See section "building blocks of DDD" in http://en.wikipedia.org/wiki/Domain-driven_design for definitions of Entity and Service ;) – Ocramius Feb 12 '13 at 17:39
  • Check this [solution][1]. You have both the solutions that I can think of. [1]: http://stackoverflow.com/questions/12770966/service-locator-in-zend-framework-2 – Pradeep Feb 12 '13 at 22:56
  • @Ocramius I don't need to extend Zend FORM or table because I am not using them is there a Zend Module that I can extend , with out extending If I cant implement I guess – Aravind.HU Feb 13 '13 at 01:05

3 Answers3

3

Assuming that you build your service (your code looks like a service) you will probably instantiate it in a service factory (in this case I've put it in the module config):

class MyModule
{
    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'my_request_object' => function (
                    \Zend\ServiceManager\ServiceLocatorInterface $sl
                ) {
                    $config = $sl->get('config'); 

                    return new \GaGooGl\Http\Request($config);
                },
            ),
        );
    }
}

This way, you are injecting the config object directly in its consumer (without having a reference to the service locator in the consumer)

Another way is to implement Zend\ServiceManager\ServiceLocatorAwareInterface in your GaGooGl\Http\Request. I personally discourage it, but this basically allows you to have your Request object keep a reference to the service locator internally, therefore making it possible to retrieve the config service at runtime.

Ocramius
  • 25,171
  • 7
  • 103
  • 107
  • Do we need to define this in module.php file – Aravind.HU Feb 12 '13 at 17:40
  • How to get the config object with in the model class actually I agree you are saying to return config object . – Aravind.HU Feb 12 '13 at 17:41
  • You cannot have the config object without having access to the service locator. Either pass the config object in the factory (that closure I wrote above) to your `Request` object, or pass the `$sl` to the `Request` object (so that then the `Request` object can access it to get the `'config'` service) – Ocramius Feb 12 '13 at 18:59
  • I don't need to extend Zend FORM or AbstractTableGatewa because I am not using them is there a Zend Module that I can extend , with out extending I cant implement I guess it throws an error , – Aravind.HU Feb 13 '13 at 01:06
  • You are wrong this will return module configuration , not global configuration , I am not getting global configuration . – Aravind.HU Feb 13 '13 at 13:45
  • This is the globally merged config – Ocramius Feb 13 '13 at 13:49
1

simplest way to do this

$config = new \Zend\Config\Config( include APPLICATION_PATH.'/config/autoload/global.php' ); 
Saurabh Chandra Patel
  • 12,712
  • 6
  • 88
  • 78
  • 3
    I would definitely call this a *hack*. – Alex Nov 27 '15 at 09:56
  • I would say a working hack, isn't it ? Maybe `include_once` and not `include` could be better. And maybe using the factory to load the configuration file could be even better. @see https://framework.zend.com/manual/2.4/en/modules/zend.config.factory.html – BendaThierry.com Jun 12 '19 at 18:43
0

Check this. It has two solutions. One is implementing a Service locator aware interface. Another is to inject the service manager into your model. For both, you need to instantiate your model object through the service manager.

Community
  • 1
  • 1
Pradeep
  • 2,469
  • 1
  • 18
  • 27