2017-07-30 83 views
2

我想訪問我的控制器內的服務定位器對象,但無法做到這一點。
我試圖聯機幫助,但他們大多正在跟蹤ZF2

如何在ZF3中訪問控制器內的服務定位器對象?

以前在zf2服務定位器訪問方式是一件輕而易舉的事,我只是做$這個 - > getServiceLocator();

我已經嘗試爲我的控制器創建工廠類,並在那裏創建createService方法,但它說我也必須實現__invoke()方法。

我的目標是做這樣的事情

public function getPropertyTable() 
{ 
    if (!$this->PropertyTable) { 
     $sm = $this->getServiceLocator(); 
     $this->PropertyTable = $sm->get('Application\Model\PropertyTable'); 
    } 
    return $this->PropertyTable; 
} 


誰能給我一個完整的步驟來實現這一目標?

我試圖問這個問題之前,實現幾乎所有涉及到的ServiceLocator的答案,所以請幫我標誌着這個作爲複製或某件事之前,忽略了錯別字

+0

[Zend Framework 3中的GetServiceLocator]的可能重複(https://stackoverflow.com/questions/42974794/getservicelocator-in-zend-framework-3) – akond

+2

在ZF2中,確定在控制器中使用Service locator是一個非常非常糟糕壞壞的做法。這是不可能/建議在ZF3中再次使用該技巧 – Hooli

+1

@Hooli謝謝,我完全理解並同意您的觀點,我知道這就是爲什麼ZF3中服務經理的整個流程發生了變化的原因。但這就是我提出這個問題的原因,我不知道如何在控制器內部調用我的模型的函數。請看我是一位正在學習這項技術的新手。我希望如果你能給我一個例子,我怎樣才能在Controller中調用模型函數。 –

回答

3

謝謝大家告訴我我做錯了。
關於這個主題的一些更多的研究,幫我把我的問題解決了
這裏是我做了什麼來解決它

  • 爲控制器
    創建工廠類你必須爲你的控制器工廠類將實現Zend的FactoryInterface。 在那裏你必須調用

    public function __invoke(ContainerInterface $container, $requestedName, array $options = null) 
    { 
         return new ListController($container->get(PropertyInterface::class)); 
    } 
    


    這裏我傳遞這是由我稱爲房產表另一個表中,我已經給身體我所有inteface功能模型
    像searchProperty實施PropertyInterface refrence ()

  • 加工廠類在配置文件中module.config。PHP的爲您的控制器
    指示配置文件,讓我們的工廠創建對象爲我們的控制器

    'controllers' => [ 
          'factories' => [ 
           // Update the following line: 
           Controller\ListController::class => Factory\ListControllerFactory::class, 
          ], 
         ], 
    


  • 註冊您的配置文件模式
    您必須添加新服務經理部分,並在那裏提供你的模型類。

    // Add this section: 
         'service_manager' => [ 
          'aliases' => [ 
           Model\PropertyInterface::class => Model\PropertyTable::class, 
          ], 
          'factories' => [ 
           Model\PropertyTable::class => InvokableFactory::class, 
          ], 
         ], 
    



  • 現在唯一剩下的就是在PropertyTable添加功能在你的PropertyInterface和Implentation他們,然後叫他們在你的控制器

    Complete Steps for implementation幫我在實施新的流程。
    感謝社區。你們都是最好的。

    +0

    很高興你終於解決了你的問題,如果我的答案幫助你解決了你的問題,upvote可以很好的方式... – Hooli

    1

    只要在ZF3新工廠接口:

    interface FactoryInterface 
    { 
        /** 
        * Create an object 
        * 
        * @param ContainerInterface $container 
        * @param string    $requestedName 
        * @param null|array   $options 
        * @return object 
        */ 
        public function __invoke(ContainerInterface $container, $requestedName, array $options = null); 
    } 
    

    你必須像這樣實現你的工廠。

    幫助你,你可以使用link

    編輯:

    在實踐中,你應該有這樣的:

    public function __invoke(ContainerInterface $container, $requestedName, array $options = null) 
    { 
        $propertyTable = $container->get('Application\Model\PropertyTable'); 
        return new Controller($propertyTable); 
    } 
    
    +1

    感謝您指點我正確的方向, 我在網上查找有關您的答案,並發現**這個** [鏈接](https://docs.zendframework.com/tutorials/in-depth-guide/models -and-servicemanager /#writing-a-factory-class) 我從這個鏈接實現了代碼,在這裏取得了相當的成功。只有跟隨此代碼的問題是現在我無法訪問我的sql查詢的適配器對象。 你能幫我一下,在我的PropertyTable模型中,我該如何注入Zend \ Db \ Adapter \ Adapter類對象。所以我可以像這樣使用它: '$ sql = new Sql($ this-> adapter);' –

    +1

    @PankajJha編輯 – Hooli

    -1

    在ZF3不建議以服務定位器傳遞到控制器。 您需要從控制器的工廠內的$ container獲取所有依賴關係,並通過構造函數(或setter)將它們傳遞到控制器中。

    +0

    你能告訴我一個使用setter的例子嗎?或任何鏈接 – Athar

    +0

    @Athar只需在這裏或谷歌搜索中輸入「php依賴注入設置器」。關於這個話題有很多文章。 –