2016-08-18 15 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', 

      ], 

     ],