2013-10-03 45 views
0

我正在尋找一個模塊。當我搜索一些項目時,該項目的slu is在URL中作爲查詢字符串傳遞,但是我想將其轉換爲命名參數。爲此,我在自定義組件RedirectComponent.php中創建了一個urlToNamed()操作。我的代碼是:cakephp重定向在控制器不起作用

RedirectComponent.php 

function urlToNamed() { 
    $urlArray = $this->controller->params->query; 
    if(!empty($urlArray)){ 
     #remove unset values 
     $urlArray = array_filter($urlArray); 
     $this->controller->redirect($urlArray); 
    } 
}  

I am calling the urlToNamed() action from index action of BooksController.php i.e 


public function index() { 
    $this->Redirect->urlToNamed(); 

    //All other stuff to handle named parameter data and show result. 
} 

問題是我的網址搜索之後的數據是作爲查詢字符串像http://localhost/esolutions/books/index?category=Books,而我必須轉換到一個名爲參數一樣http://localhost/esolutions/books/index/category:Books

另一個問題是,

$this->controller->redirect($urlArray); 

不工作。請給出任何建議。提前致謝。

+0

爲什麼你需要將查詢字符串參數轉換爲命名參數?您也可以在控制器操作中使用查詢字符串參數。 –

+0

實際上,我通過使用slug(帶有命名參數)在BooksController中顯示所有產品。這就是爲什麼我不想改變搜索功能。 – yolla

回答

0

如果您需要查詢字符串參數改變給定參數,那麼我只是把這個功能您index()行動:

public function index($category = null) { 
    if (isset($this->request->query['category'])) { 
     $this->redirect(array('category' => $this->request->query['category']), 301); 
    } 

    // rest of index code as normal 
} 

將執行301重定向任何URL像/esolutions/books/index /?category = foo/esolutions/books/index/category:foo

+0

感謝Martin的努力。你的代碼正在工作,現在我發佈的代碼也在工作。其實問題是重定向功能沒有在我的控制器工作。但現在很好。 – yolla

+0

很高興爲您服務。 –