2016-08-18 16 views
0

我想要什麼的Yii 2.不同的行動取決於傳遞的參數

我想調用不同的動作對此類URL

abc.com/ — Home page 

abc.com/<argument-1>/<argument-2> — Search page 

我有什麼

這裏是我的web.php路由配置和使用索引操作進行搜索。

'urlManager' => [ 
      'enablePrettyUrl'  => true, 
      'showScriptName'  => false, 
      'enableStrictParsing' => false, 
      'rules'    => [ 
       //Home page 
       '/' => 'site/index', 
       //Search 
       '<tag>/<location>' => 'site/search', 
      ], 
     ], 

而在abc.com/我得到一個無限循環。

Index操作

/** 
    * Displays homepage. 
    * 
    * @return string 
    */ 
    public function actionIndex() 
    { 
     return $this->render('index', ['ip-info' => Locator::getLocation()]); 
    } 

搜索行動

public function actionSearch(
    array $tag = ['any'], 
    array $location = ['any'], 
    $display = 'list', 
    $sort = 'name' 
) { 

    //... 
    //some actions to fill the variables. 
    //... 

    return $this->render('search', [ 
     'data'   => $data, 
     'display-type' => $display, 
     'sidebar'  => $sidebar, 
     'countries' => $location, 
     'sort'   => $sort, 
     'title'  => $title, 
    ]); 
} 
+0

你能發佈你的搜索動作 – g9m29

+0

什麼代碼寫在索引操作?請檢查 –

+0

如果您從abc.com/中刪除/ abc.com,只需嘗試abc.com並檢查視圖,即索引是否有任何重定向代碼? –

回答

0

問題是在組合情況。 AJAX產生錯誤的路由請求,最近的catch提供一個循環。這是工作規則的例子。

'urlManager' => [ 
      'baseUrl' => '/', 

      'enablePrettyUrl'  => true, 
      'showScriptName'  => false, 
      'enableStrictParsing' => true, 
      'rules' => [ 
       //Home page 
       '/'     => 'site/index', 

       //AJAX 
       'ajax/<action:\w+>' => 'ajax/<action>', 


       //Search 
       '<tag:\w+>/<location:\w+>' => 'site/search', 

      ], 

     ],