簡體   English   中英

ZF2:如何在自定義路由器中獲取Zend \\ Db \\ Adapter?

[英]ZF2: How to get Zend\Db\Adapter inside custom router?

在標題中,我很難在Router中訪問DBAdapter。 實現ServiceLocatorAwareInterface沒有多大幫助(ZF2沒有注入任何東西)。 在具有自定義工廠的模塊中將其聲明為服務也不是一種選擇,因為它擴展了Http / Parts路由器並且需要根據路由傳遞配置參數(我不想對它們進行硬編碼)

我已經嘗試過的:

module.config.php:

(...)
'router' => array(
    'routes' => array(
        'adm' => array(
            'type'    => 'Custom\Mvc\Router\Http\Segment',
            'options' => array(
                'route'    => '/admin[/:language[/:controller[/:action[/:params]]]]',
                'constraints' => array(
                    'language'  => '(pl|en)',
                    'controller' => "[a-zA-Z0-9_\-]*",
                    'action'     => "[a-zA-Z0-9_\-]*",
                    'params'        => "(.*)",
                ),
                'defaults' => array( ... ),
            ),
            'may_terminate' => true,
        ),
    ),
),
'service_manager' => array(
     (...)
    'invokables' => array(
        'Custom\Mvc\Router\Http\Segment' => 'Custom\Mvc\Router\Http\Segment',
    ),
),
(...)

截至目前,Custom \\ Mvc \\ Router \\ Http \\ Segment只是Zend \\ Mvc \\ Router \\ Http \\ Segment的副本,添加了接口ServiceLocatorAwareInterface,AdapterAwareInterface以及類似方式的相應方法:

public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
    var_dump($serviceLocator);
    exit();
}

它從不進入setServiceLocator方法,只進入RouteInterface :: factory(),然后調用構造函數。

設置工廠也沒有幫助 - 代碼沒有執行。 將'invocables'或factory移動到應用程序配置后的行為相同。

目前使用Zend Framework 2 RC1

如果你想讓我們獲取som代碼會更容易.. :)

我的建議是使用工廠來實現自定義路由器或將其設置為可調用類(需要您實現ServiceLocatorAwareInterface,以便您可以在路由器中進行設置)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM