2012-10-25 13 views
8

成功していない長い時間を検索した後。 私はあきらめる前に、Zend Framework 2サブドメインをモジュールにルーティングする

サブドメインをモジュールにルーティングする方法はありますかZend Framework 2?以下のような:

サブドメイン =>モジュール
api.site.com => API
dev.site.com => DEV
admin.site.com =>管理者
site.com = >公共
...

私はこのようにそれをやってみましたが、私はデフォルト(インデックス)以外のコントローラへのアクセスを得ることができません。

'router' => array(
    'routes' => array(
     'home' => array(
      'type' => 'Hostname', 
      'options' => array(
       'route' => 'site.com', 
       'defaults' => array(
        '__NAMESPACE__' => 'Application\Controller', 
        'controller' => 'Index', 
        'action'  => 'index', 
       ), 
      ) 
     ) 
    ), 
), 

ご協力いただきありがとうございます。

答えて

5

Zend Framework 2には、モジュールへのルーティングという概念はありません。すべてのルーティングマッピングは、URIパターン(HTTPルートの場合)と特定のコントローラクラスの間にあります。つまり、Zend\Mvcはイベントリスナー(Zend\Mvc\ModuleRouteListener)を提供しています。これにより、与えられたパターンに基づいて複数のコントローラにマップするURIパターンを定義し、「モジュールルーティング」をエミュレートできます。このような経路を定義するには、あなたのルーティング設定としてこれを置きます:

'router' => array(
    'routes' => array(
     // This defines the hostname route which forms the base 
     // of each "child" route 
     'home' => array(
      'type' => 'Hostname', 
      'options' => array(
       'route' => 'site.com', 
       'defaults' => array(
        '__NAMESPACE__' => 'Application\Controller', 
        'controller' => 'Index', 
        'action'  => 'index', 
       ), 
      ), 
      'may_terminate' => true, 
      'child_routes' => array(
       // This Segment route captures the requested controller 
       // and action from the URI and, through ModuleRouteListener, 
       // selects the correct controller class to use 
       'default' => array(
        'type' => 'Segment', 
        'options' => array(
         'route' => '/[:controller[/:action]]', 
         'constraints' => array(
          'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 
          'action'  => '[a-zA-Z][a-zA-Z0-9_-]*', 
         ), 
         'defaults' => array(
          'controller' => 'Index', 
          'action'  => 'index', 
         ), 
        ), 
       ), 
      ), 
     ), 
    ), 
), 

Click here to see an example of this @ ZendSkeletonApplication

をこれは、しかし、方程式の半分だけです。特定の命名形式を使用して、モジュール内のすべてのコントローラクラスも登録する必要があります。これは、同じ構成ファイルを介して行われます:

'controllers' => array(
    'invokables' => array(
     'Application\Controller\Index' => 'Application\Controller\IndexController' 
    ), 
), 

配列のキーがModuleRouteListenerは、右のコントローラを見つけるために使用する別名であり、それは次の形式にする必要があります

<Namespace>\<Controller>\<Action> 

値この配列キーに割り当てられるのは、コントローラクラスの完全修飾名です。

Click here to see an example of this @ ZendSkeletonApplication

注:ZendSkeletonApplicationを使用していない、またはそれがデフォルトアプリケーション・モジュールの取り外しを行った場合は、あなたがあなた自身のモジュールの1つにModuleRouteListenerを登録する必要があります。 Click here to see an example of how ZendSkeletonApplication registers this listener

+2

明確で正確な説明をいただきありがとうございます。私はあなたの解決策を取った。私はこれでずっと苦労しました。 – Sapher

+0

それは素晴らしかった、ありがとう。 – cr125rider

+0

私はmamp proを使用し、仮想ホスト名にサブドメインを作成します。したがって、reseller.myhost.com/testを使用した後はokeyですが、reseller.myhostと書いてください。私のモジュールのindexactionではないインデックスに行く –

4

私が正しくDASPRIDS Rounter Presentationの#39をスライド理解していれば、それは同じくらい簡単です - モジュールごとに - サブドメインのホスト、すなわちを定義するには:

'router' => array(
    'routes' => array(
     'home' => array(
      'type' => 'Hostname', 
      'options' => array(
       'route' => 'api.site.com', 
       'defaults' => array(
        '__NAMESPACE__' => 'Api\Controller', 
        'controller' => 'Index', 
        'action'  => 'index', 
       ), 
      ) 
     ) 
    ), 
), 

その他、各種、あなたはすべてのモジュールのためにこれを行うだろうそのままで。

+0

残念ですが、apiサブドメインの新しいWebサーバー仮想ホストを登録する必要がありますか? –

関連する問題