2012-06-07 76 views
2

我想在Zend Framework 2中創建一個通用模塊/控制器/動作路由,以便與ZF2 MVC體系結構一起使用。如何在Zend Framework 2中創建通用模塊/控制器/動作路由?

在ZF1中,默認路由被定義爲/[:module][/:controller][/:action],其中模塊默認爲default,控制器默認爲index,動作爲index

現在,ZF2改變了模塊從簡單的控制器和視圖組到控制器名稱到控制器類的顯式映射的實際獨立應用程序的方式。

由於所有的控制器名稱必須在所有模塊唯一的,我想他們的名字一樣modulename-controllername但我想的URL看起來像/modulename/controllername,而不需要爲每個模塊創建特定的路線,使用類似舊默認上述ZF1的路線。

回答

8

是的,它是非常有可能的,但你必須做一些工作。請使用以下配置:

 'default' => array(
      'type' => 'My\Route\Matcher', 
      'options' => array(
       'route' => '/[:module][/:controller[/:action]]', 
       'constraints' => array(
        'module' => '[a-zA-Z][a-zA-Z0-9_-]*', 
        'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 
        'action'  => '[a-zA-Z][a-zA-Z0-9_-]*', 
       ), 
       'defaults' => array(
        'module'  => 'default', 
        'controller' => 'index', 
        'action'  => 'index', 
       ), 
      ), 
     ), 

然後,你必須寫自己的My\Route\Matcher創建的RouteMap對象的MVC可以使用。這並不難,看看框架中已有的其他路由匹配器,你就會明白這一點。

+0

我會試試這個,看看我能否正常工作。 –

+0

你有沒有得到這個工作,你可以分享匹配代碼? –

+0

@IvoJansch:對不起,但目前我不再在這個項目上工作,因爲沒有空閒時間在這個項目上工作...... –

1

如果您使用Zend主幹應用程序已經配置了此默認控制器。

看到這裏https://github.com/zendframework/ZendSkeletonApplication/blob/master/module/Application/config/module.config.php

+1

我知道,默認的控制器已經被配置,我嘗試做我我想設置一個路由,例如,模塊「bar」中的控制器「foo」將被路由「/ bar/foo」匹配,同時在控制器列表中註冊爲「bar-foo 」。 另外我希望用來做這個匹配的路由是通用的,這樣我就不需要爲每個模塊定義一個。 –

0

爲了有一個ZF2模塊的通用/標準路由系統,這是我只是一個Controller 「模塊\控制器\指數」 的解決方案(默認控制器):

'router' => array(
    'routes' => array(    
     'default' => array(
      'type' => 'Literal', 
      'options' => array(
       'route' => '/', // <======== this is take the first step to our module "profil" 
       'defaults' => array(
        'module'  => 'profil', 
        'controller' => 'profil\Controller\Index', 
        'action'  => 'index', 
       ), 
      ), 
     ),    
     'profil' => array(
      'type' => 'Segment', 
      'options' => array(
       'route' => '/[profil][/:action]', // <======== this is take the next steps of the module "profil" 
       'constraints' => array(
        'module' => '[a-zA-Z][a-zA-Z0-9_-]*', 
        'action'  => '[a-zA-Z][a-zA-Z0-9_-]*', 
       ), 
       'defaults' => array(// force the default one 
        'module'  => 'profil', 
        'controller' => 'profil\Controller\Index', 
        'action'  => 'index', 
       ), 
      ), 
     ), 
    ), 
), 

然後在我們的控制器「PROFIL \控制器\目錄」,我們有三項行動‘索引’‘家’‘signout’:

public function indexAction() 
{ 
     if ($this->identity()) { 
      return $this->redirect()->toRoute('profil',array('action'=>'home')); 
     } else { 
      // ...... 
        $authResult = $authService->authenticate(); 
        if ($authResult->isValid()) { 
          //...... 
                return $this->redirect()->toRoute('profil',array('action'=>'home')); 
        } else { 
         // ...... 
        } 
       } else { 
        $messages = $form->getMessages(); 
       } 
      }    
      return new ViewModel(); 
     } 
} 

public function homeAction() 
{ 
    if (!$this->identity()) { 
     return $this->redirect()->toRoute('profil',array('action'=>'signout')); 
    } 
} 

public function signoutAction() 
{ 
    if ($this->identity()) { 
     $authService = $this->getServiceLocator()->get('Zend\Authentication\AuthenticationService'); 
     $authService->clearIdentity(); 
    } 
    $this->redirect()->toRoute('profil'); 
} 

,並謝謝你的好意:)

相關問題