2013-10-16 35 views
1

我試圖解決我所面臨現在沒有運氣(GOOGLE上搜索了幾個小時的路由問題,看到了幾十個例子,解決了問題,但沒有對我的作品 - 最近的一個是這Routing Zend Framework 2 Language in url但即使這不適合我)。Zend框架2 - <home>航線後路由參數

我已經創建了一個SSO應用程序(我的意思是驗證部分),現在我將它移植到ZF2應用程序中(我幾乎可以工作,因爲我在改變路由器的工作方式,但現在需要完成最終路由)類型的網址是可能的:

HTTP [秒]://domain.com/login/asfgsdgdsfgzsdgdsf/
HTTP [秒]://domain.com/login/tdhjsdgbndfnfgdnhd/
HTTP [s]的://domain.com/logout/asfgsdgdsfgzsdgdsf/
http [s]://domain.com/info/dthnzdfbdfhgnfsd/

那些loginlogoutinfo部分不是控制器,也沒有行動,但這些方法的名字然後我從我的IndexControllerindexAction()內的SSO服務調用。 URL的其餘部分是客戶端應用程序哈希。

現在這裏是我的路由器配置,只匹配home路由,當其他部分(方法名稱[和散列])提供時,我從ZF2應用程序獲得404(所以至少我在應用程序上下文中) 。

return array(
    'router' => array(
     'routes' => array(
      'home' => array(
       'type'  => 'Zend\Mvc\Router\Http\Segment', 
       'options' => array(
        'route'  => '/', 
        'defaults' => array(
         'controller' => 'SSO\Controller\Index', 
         'action'  => 'index', 
         'act'   => 'login', // by doing this I am able to workaround the router and work with SSO 
         'client'  => '<my_secret_hash>', // by doing this I am able to workaround the router and work with SSO 
        ), 
       ), 
       'child_routes' => array(
        'action' => array(
         'type'  => 'Zend\Mvc\Router\Http\Segment', 
         'options' => array(
          'route'  => '[/:act]', 
          'defaults' => array(
           'act' => 'login', 
          ), 
          'constraints' => array(
           'act' => '(login|logout|info)?', // only login, logout or info are allowed 
          ), 
         ), 
         'child_routes' => array(
          'client' => array(
           'type'  => 'Zend\Mvc\Router\Http\Segment', 
           'options' => array(
            'route'  => '[/:client]', 
            'constraints' => array(
             'client' => '[a-zA-Z0-9]+', 
            ), 
            'defaults' => array(
             'client' => '<my_secret_hash>', 
           ), 
           'may_terminate' => true, 
          ), 
         ), 
        ), 
       ), 
      ), 
     ), 
    ), 
); 

(僅routermodule.config的一部分提供...)

現在與路由器只http[s]://domain.com[/]匹配和任http[s]://domain.com/(login|logout|info)[/]http[s]://domain.com/(login|logout|info)/adfbsDVdfbzdbxfbndzxbxfnb[/]比賽進入404錯誤發生

雖然我嘗試定義爲可選的(只是爲了測試和開發目的)的路由部分,他們應要求在生產環境中。無論如何,我試圖定義它們不是可選的,但也沒有工作。

問題:我應該如何配置路由器以匹配我在開始時定義的路由(URL)?

回答

3

幾件事情:

  • 我的猜測是,你在那裏有太多的斜槓 - 的/孩子的路線並不需要與開頭斜線來定義,因爲它的母公司已經有了它
  • 我沒有看到2條嵌套路線的理由(當然我不知道你的配置的其餘部分,所以這是一個猜測)
  • home路線大概應該是Literal並且能夠終止(再次猜)
  • 有一個無與倫比的括號(可能是 'misspasted' 或某事)

我想這應該爲你工作:

'router' => array(
    'routes' => array(
     'home' => array(
      'type' => 'Literal', 
      'may_terminate' => true, 
      'options' => array(
       'route'  => '/', 
       'defaults' => array(
        'controller' => 'SSO\Controller\Index', 
        'action'  => 'index', 
       ), 
      ), 
      'child_routes' => array(
       'whateva' => array(
        'type'  => 'Segment', 
        'may_terminate' => true, 
        'options' => array(
         'route'  => '[:act[/:client]]', // as Sam suggested 
         'defaults' => array(
          'act' => 'login', 
          'client' => 'fsadfasf32454g43g43543', 
         ), 
         'constraints' => array(
          'act' => '(login|logout|info)', 
          'client' => '[a-zA-Z0-9]+', 
         ), 
        ), 
       ), 
      ), 
     ), 
    ), 
), 
+2

很好的回答,一個小姐:'[:行爲[/:客戶端] ' - 客戶端參數應該只在給出了行爲參數時才存在。你不希望客戶沒有采取任何行動,呃;) – Sam

+0

很好的答案,非常感謝。我確信我也試過這個(或者非常類似的東西),但是可能會發生一行不同的結果,它不起作用......我對這個「may_terminate」設置感到好奇 - 它究竟做了什麼?在我的應用程序中,沒有'act'和'client'參數的home('/')應該導入'404 No action found'並且如果'401 Unauthorized'沒有提供'client'參數。所以我想我的家('/')不應該被設置爲'may_terminate',如果我沒有錯的話...... – shadyyx