2012-06-01 29 views
0

我正在一個網站,其中IndexController持有像「關於」,「聯繫」等一些網頁我希望Zend或.htacess配置重定向/約/ index /約,但沒有URL重寫(至少對用戶來說是透明的)。如何將動作重定向爲Zend中的「模擬」控制器?

所以它會像這樣進行的東西:

mysite.com/about => mysite的/索引/約(W/O表示所述URI給用戶)。

回答

1

你不需要任何特別的事情就可以做到這一點,ZF2是完全有能力處理自定義路線。 ZF2 tutorial by Akrabat顯示得很好。最終你會在你的配置做這樣的事情:

'Zend\Mvc\Router\RouteStack' => array(
    'parameters' => array(
     'routes' => array(
      'default' => array(
       'type' => 'Zend\Mvc\Router\Http\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' => 'Application\Controller\IndexController', 
         'action'  => 'index', 
        ), 
       ), 
      ), 
      'about' => array(
       'type' => 'Zend\Mvc\Router\Http\Literal', 
       'options' => array(
        'route' => '/', 
        'defaults' => array(
         'controller' => 'Application\Controller\IndexController', 
         'action'  => 'about', 
        ), 
       ), 
      ), 
     ) 
    ) 
) 

當然你也可以通過擴展ActionController的也可以通過寫控制器插件使自動化這些東西。

+0

我寧願使用一個解決方案,將避免發明一個新的控制器,這些似乎相當高效! 我現在就試試。 – jackyalcine

+0

等待,在哪個配置?不是我的應用程序/ configs/application.ini,不是? – jackyalcine

+1

在模塊\ Application \ config \ module.config.php中。正如我所說,通過Akrabat教程。 – markus

-1

嘗試在適當的地方在你的文檔根目錄添加這.htaccess文件:

RewriteEngine On 
RewriteRule ^about(.*)$ /index/about$1 [L] 
+0

我會非常反對通過Apache解決這個問題的建議! – markus

2

使用新的服務管理器的配置:

'controller' => array(
    'classes' => array(
     'index_controller' => 'MyModule\Controller\IndexController', 
    ), 
), 

'router' => array(
'routes' => array(

    'activities_list' => array(
     'type' => 'Literal', 
      'options' => array(
      'route' => '/about', <- The requested URL 
      'defaults' => array(
       'controller' => 'index_controller', <- What will process the request 
       'action'  => 'about', 
      ), 
     ), 
    ), 

), // End of routes 
), // End of router 
+0

您引用的新服務管理器將是Zend_ServiceManager,不是嗎?如果我想將它放在我的IndexController :: init()方法中,它將如何顯示? 此外,你知道任何文件,進一步解釋這一點?我發現主要是樣板代碼。 – jackyalcine

+0

以下配置轉到/module/myModule/config/module.config.php。在控制器中,您可以:$ this-> getServiceLocator()來獲取服務管理器 – michaelbn

相關問題