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

  • SEARCH
  • Home
  • 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 8676737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:16:24+00:00 2026-06-12T20:16:24+00:00

In controller I create and use my model so public function getAlbumTable() { if

  • 0

In controller I create and use my model so

public function getAlbumTable()
{
    if (!$this->albumTable) {
        $sm = $this->getServiceLocator();
        $this->albumTable = $sm->get('Album\Model\AlbumTable');
    }
    return $this->albumTable;
}

How do I use this global Service Locator in another place of my project, for example, in the other model, and not only in any controller?

Сonfiguration of the connection to the database is defined in the file: my_project/config/autoload/global.php

Thank you.

  • 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-12T20:16:25+00:00Added an answer on June 12, 2026 at 8:16 pm

    Decided. So. For solving the task of classes of models must implement the interface ServiceLocatorAwareInterface. So injection ServiceManager will happen in your model automatically. See the previous example.

    For forms and other objects of your application suitable method proposed here http://michaelgallego.fr/blog/?p=205 You can to create a base class form extends BaseForm and implements ServiceManagerAwareInterface, from which you will inherit its forms in the application.

    namespace Common\Form;
    
    use Zend\Form\Form as BaseForm;
    use Zend\ServiceManager\ServiceManager;
    use Zend\ServiceManager\ServiceManagerAwareInterface;
    
    class Form extends BaseForm implements ServiceManagerAwareInterface
    {
        /**
         * @var ServiceManager
         */
        protected $serviceManager;
    
        /**
         * Init the form
         */
        public function init()
        {
        }
    
        /**
         * @param ServiceManager $serviceManager
         * @return Form
         */
        public function setServiceManager(ServiceManager $serviceManager)
        {
            $this->serviceManager = $serviceManager;
    
            // Call the init function of the form once the service manager is set
            $this->init();
    
            return $this;
        }
    }
    

    To injection of the object of the ServiceManager was automatically in the file module.config.php in section service_manager you need to write

    'invokables' => array(
        'Album\Form\AlbumForm' => 'Album\Form\AlbumForm',
    ),
    

    Then in your controller, you can create a form so

    $form = $this->getServiceLocator()->get('Album\Form\AlbumForm');
    

    The form will contain an object ServiceManager, which will allow other dependencies.

    Thanks all for your help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my controller: public ActionResult Create() { Number newNumber = new Number(); return
Is this right way to do: I create a model, controller, view for localhost/users
I have a create action in the controller that I use for a form
I have a problem to send my Model into my controller. I use a
I've got 2 controllers. CategoryController public function readAction() { $id = $this->_request->getParam('id'); $categoryModel =
I have a model class like this: namespace Models { public struct Localization {
This is my view: @model PhoneBook.Models.Number @{ ViewBag.Title = Create; } <h2>Create</h2> <script src=../../Scripts/jQuery.AddNewNumber.js
I am trying to use pagination for blog articles on my homepage: Controller: public
I am trying to create an admin instance through my admins controller create action,
If i have a controller action Create that returns a view with the following

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.