-1

I am getting below error message while using service manager. How can i resolve this via different approach like constuct....

Deprecated: You are retrieving the service locator from within the class Users\Controller\LoginController. Please be aware that ServiceLocatorAwareInterface is deprecated and will be removed in version 3.0, along with the ServiceLocatorAwareInitializer. You will need to update your class to accept all dependencies at creation, either via constructor arguments or setters, and use a factory to perform the injections. in C:\wamp64\www\ZendSkeletonApplication-master\vendor\zendframework\zend-mvc\src\Controller\AbstractController.php on line 258

Below code i have added in module.php

public function getServiceConfig() {
        return array(
            'abstract_factories' => array(),
            'aliases' => array(),
            'factories' => array(
                // FORMS
                'LoginForm' => function ($sm) {
                    $form = new \Users\Form\LoginForm();
                    $form->setInputFilter($sm->get('LoginFilter'));
                    return $form;
                },
            )
        )
}

and from login controller, index action i calling below code

$form = $this->getServiceLocator()->get('LoginForm');
$viewModel = new ViewModel(array('form' => $form));
return $viewModel;

Any help is highly appreciated.

Currently i am using Zend framework 2.5.1 Version In Zend framework 2.3 Version it was working fine.

Update

Now i am using below code in my controller

 // Add this property:
    private $table;

    // Add this constructor:
    public function __construct(LoginForm $table) {
        $this->table = $table;
    }

and in module.php

 // FORMS
                Model\AlbumTable::class => function ($sm) {
                    $form = new \Users\Form\LoginForm();
                    $form->setInputFilter($sm->get('LoginFilter'));
                    return Model\AlbumTable;
                },

But still i am getting below error

Catchable fatal error: Argument 1 passed to Users\Controller\LoginController::__construct() must be an instance of Users\Form\LoginForm, none given, called in C:\wamp64\www\ZendSkeletonApplication-master\vendor\zendframework\zend-servicemanager\src\AbstractPluginManager.php on line 252 and defined in C:\wamp64\www\ZendSkeletonApplication-master\module\Users\src\Users\Controller\LoginController.php on line 22

Harsh
  • 81
  • 7
  • 2
    WHats the question? Do you want to know what deprecated means? Do you want to know how to stop the error message appearing? Do you want to know what to use instead? – RiggsFolly Aug 03 '16 at 08:29
  • I am getting above error message while using service manager. How can i resolve this via different approach like construct.... – Harsh Aug 03 '16 at 08:31
  • 3
    Follow the instructions in the message. And look at this http://stackoverflow.com/questions/36061210/deprecated-retrieve-service-locator-in-functional-system-zf2 A 10 second google found me that. If that is all you need, delete this question as its obviously a duplicate – RiggsFolly Aug 03 '16 at 08:34
  • I have already gone through the url but not able to write the code – Harsh Aug 03 '16 at 08:38
  • It still works fine and will until you upgrade to ZF3, did you not read any of that link I gave you – RiggsFolly Aug 03 '16 at 08:39
  • Right it works but i do not want to use deprecated code. If there is any alternate solution please let me know – Harsh Aug 03 '16 at 08:40
  • Doesn't this answer do it for you http://stackoverflow.com/a/36061921/2310830 – RiggsFolly Aug 03 '16 at 08:42
  • I have gone through the code but i am not able to understand where i need to write what, like construct function i have writed inside controller but how can i call login form from construct function ... – Harsh Aug 03 '16 at 08:45
  • try to have a look here too, https://mwop.net/blog/2016-04-26-on-locators.html – marcosh Aug 03 '16 at 08:56
  • Question answered, need a feedback ;) – Greco Jonathan Aug 09 '16 at 14:47

2 Answers2

0

There was a lot of problem in the use of serviceLocator in ZF2, Zend tech' did a great job by removing the serviceLocatorAware from the framework, and remove the serviceManager from controllers.

Why ?

Just because some entry and experienced developpers used it in an ugly way, and way too much.

From my point of view, the serviceLocator is meant to be used only in factories.

That's why i keep advising other developper to create factories, without using anonymous function.

Here an example of a controller's factory (not the same as service's factories) : https://github.com/Grafikart/BlogMVC/blob/master/ZendFramework2/module/Blog/src/Blog/Factory/PostControllerFactory.php

And its config line https://github.com/Grafikart/BlogMVC/blob/master/ZendFramework2/module/Blog/config/module.config.controllers.php#L8

And here a Service's factory

<?php
namespace Blog\Factory;
use Blog\Service\CategoryService;
use Doctrine\Common\Persistence\ObjectManager;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class CategoryServiceFactory implements FactoryInterface
{
    /**
     * @param  ServiceLocatorInterface $serviceLocator
     * @return CategoryService
     */
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        /** @var ObjectManager $em */
        $em = $serviceLocator->get('orm_em');
        return new CategoryService($em);
    }
}

You can do a factory for almost all of your component, even form, you just need to declare those as factories in your config like this :

You can replace the key form_elements by :

  • controllers
  • service_manager
  • view_helpers
  • validators

It will works the same way :

'form_elements'   => array( 
        'factories' => array(
            'Application\Item\Form\Fieldset\ProfileFieldset' =>
                'Application\Item\Factory\ProfileFieldsetFactory',
        ),
        'invokables' => array(
            'EntityForm'   => 'Application\Entities\Form\EntityForm',
            'PropertyForm' => 'Application\Item\Form\PropertyForm',
            'ProfileForm'  => 'Application\Item\Form\ProfileForm',
        ),
        'initializers' => array(
            'ObjectManagerInitializer' => 'Application\Initializers\ObjectManagerInitializer',
        ),
    ),

Your last error means that your controller is not correctly instanciated, you not give the LoginForm instance, maybe because you didn't create a factory ? Is your controller declared as an invokables ?

Greco Jonathan
  • 2,517
  • 2
  • 29
  • 54
0

For an in depth discussion on deprecating the ServiceLocatorAwareInterface, please read this article by Matthew Weier O'Phinney. Basically, you should avoid hidden dependencies in your controllers by simply setter injecting them through factories as mentioned previously by Hooli.

Community
  • 1
  • 1
Andrew C.
  • 36
  • 3