2016-08-03 36 views
-1

我在使用服務管理器時遇到錯誤消息。 如何通過像constuct不同的方法解決這個....已棄用:ServiceLocatorAwareInterface已棄用,將在3.0版中與ServiceLocatorAwareInitializer一起被刪除

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

下面的代碼我在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; 
       }, 
      ) 
     ) 
} 

和登錄控制器,索引操作調用我下面的代碼

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

任何幫助,高度讚賞。

當前我正在使用Zend框架2.5.1版本 在Zend框架2.3版本中它工作正常。

更新

我現在用下面的代碼在我的控制器

// Add this property: 
    private $table; 

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

和module.php

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

但我仍然得到下面的錯誤我

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

+2

是什麼問題?你想知道什麼廢棄的手段?你想知道如何停止出現的錯誤信息?你想知道要使用什麼嗎? – RiggsFolly

+0

我在使用服務管理器時遇到以上錯誤消息。我怎樣才能解決這個通過不同的方法,如構造.... –

+3

請按照消息中的說明。並看看這個http://stackoverflow.com/questions/36061210/deprecated-retrieve-service-locator-in-functional-system-zf2一個10秒的谷歌找到了我。如果這是你所需要的,刪除這個問題,因爲它顯然是重複的 – RiggsFolly

回答

0

re在ZF2中使用serviceLocator時遇到了很多問題,Zend tech'通過從框架中刪除serviceLocatorAware並從控制器中刪除serviceManager做了很好的工作。

爲什麼?

僅僅因爲一些入門和經驗豐富的開發人員以一種醜陋的方式使用它,而且方式太多。

從我的角度來看,serviceLocator只能用於工廠。

這就是爲什麼我不斷建議其他開發者創建工廠,而不使用匿名函數。

這裏控制器的工廠的例子(不一樣的服務的工廠):https://github.com/Grafikart/BlogMVC/blob/master/ZendFramework2/module/Blog/src/Blog/Factory/PostControllerFactory.php

而且其配置行https://github.com/Grafikart/BlogMVC/blob/master/ZendFramework2/module/Blog/config/module.config.controllers.php#L8

在這裏服務的工廠

<?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); 
    } 
} 

你可以做幾乎所有的組件,甚至表格,你只需要在你的配置中聲明那些工廠是這樣的:

您可以通過替換鍵form_elements:

  • 控制器
  • service_manager
  • view_helpers
  • 驗證

它會以同樣的方式:

'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', 
     ), 
    ), 

你的最後錯誤意味着你的控制器沒有正確實例化,你不給LoginForm實例,也許是因爲你沒有創建工廠?您的控制器是否被聲明爲可調用的?

0

關於廢棄ServiceLocatorAwareInterface的深入討論,請閱讀Matthew Weier O'Phinney的this article。基本上,你應該避免隱藏的依賴關係在你的控制器中,通過簡單的setter注入他們的工廠,如前所述Hooli