2011-04-04 46 views
13

這裏的URL的一個例子: http://www.example.com/search/search-keyword如何通過索引功能參數笨

我努力使這項工作,我刪除使用.htaccess中的index.php,搜索是控制器,我想在索引方法中傳遞一個參數。

這是目前是什麼樣子:

class Search extends CI_Controller{ 

    function index($param){ 
     echo $param; 
    } 

} 

有什麼建議?我似乎無法使其工作

+0

您是否最終找到解決方案? – robjbrain 2011-04-06 17:21:30

回答

24
  • 您需要indexhttp://www.mysite.com/search/index/search-keyword
  • 或者你需要使用一個路由$route['search/(:any)'] = 'search/index/$1';
  • 或者你可以看看remap

切記不要相信用戶的輸入,尤其是當你把它扔到您的網址。 CI的最新版本現在支持$_GET變量,因此您可能需要考慮使用該變量或flashdata。如O' Brien這樣簡單的搜索詞會給您一個致命錯誤(「您提交的URI不允許使用字符。」)。

+0

是不是可以對查詢參數進行URL編碼,然後在控制器中對其進行解碼? – 2011-06-03 17:42:32

+0

@pat:不,你仍然會得到錯誤。然而,你可以加密參數 - 但這只是毫無意義。您也可以使用會話或發佈數據,但不會顯示在網址中。 – 2011-06-03 17:48:28

+0

ya第一個選項對我來說真的很簡單,只需在控制器名稱後面指定函數名(正如我們經常要調用其他函數一樣,例如'href =「<?php echo base_url()。」usage/index/$ selected_next_date「;?>」 ' – Dipen 2015-06-14 09:15:53

0

您應該使用CI URI類。 http://codeigniter.com/user_guide/libraries/uri.html


class Search extends CI_Controller{ 

    function index($param){ 
     //echo the search-keyword 
     echo $this->uri->segment(2); 
    } 

} 
+0

這不起作用,因爲沒有調用索引函數,它尋找不存在的函數'search-keyword' – 2011-04-05 15:50:41

+0

如何聲明uri段2作爲構造中的搜索關鍵字? – Peter 2011-04-05 21:57:09

0

這最簡單的方法就是使用uri segments

class Search extends CI_Controller{ 
    function index(){ 
    $param=$this->uri->segment(2); 
    echo $param; 
    } 
} 

或者像Madmartigan說,使用途徑,這可能是這樣做的更好的方法。

+0

這不起作用因爲索引函數沒有被調用,所以它尋找不存在的函數'search-keyword' – 2011-04-05 15:50:08

+0

很確實,你需要添加一個路由,所以我想最好只使用路由方法 – jfoucher 2011-04-06 00:23:22

+0

你可以使用php5' __call',就像你使用CI的'_remap'一樣。我個人喜歡保持我的路由乾淨和精簡,所以我喜歡那些只有一兩個簡單控制器的方法。 hings。 – 2011-04-06 00:48:42

4

您需要了解的方式代碼點火器的URL的工作,它基本上是這樣的:

http://www.mysite.com/ {控制器}/{功能}

那麼你的網址實際上是在尋找所謂的「關鍵字」功能您的搜索控制器。

您有多個選項。最簡單的將是簡單地將URL更改爲:

http://www.mysite.com/search/result/keyword

那麼這應該很好地工作:

class Search extends CI_Controller{ 

function result($param){ 
echo $param; 
} 

} 

如果你真的想使用的URL,你有了它,你可以使用與上面相同的代碼片段,但也打開「application/config/routes.php」並將其添加到底部。

$route['search/(:any)'] = "search/result"; 

另外,如果你想繼續使用索引功能,您可以將其更改爲這個

$route['search/(:any)'] = "search/index"; 

,改變你的類是這樣的:

class Search extends CI_Controller{ 

    function index($param=FALSE){ 
    if($param == FALSE) { 
     //Show search box 
    } else { 
     //Show search results 
    } 
    } 

} 

不要忘記更新你的答案,如果你弄明白自己或接受somebody的答案,如果它回答它:)

+0

這真的很酷,它的工作原理。但如何訪問傳遞給索引函數的參數? – WebDevRon 2014-03-31 02:31:21

-2

我終於找到了解決方法,因爲我無法簡單地將帖子變量放入URL中。

我所做的是創建另一個函數,然後將其重定向到該函數。

class Search extends CI_Controller{ 
    function index(){ 
     $search_item = $this->input-post('search_item'); 
     redirect("search/q/".url_title($search_item)); 
    } 

    function q($key){ 
    // process search 
    } 
} 
+0

這改變了搜索項,這是不可取的。如果我搜索了'Hello World!',你會返回'hello-world'的結果 – 2011-06-03 17:50:33

1

我剛剛開始CI,這裏是我的解決方案,並在我的網站上實現它。到目前爲止,它爲我的作品和我的搜索查詢由谷歌已經建立索引鏈接

Class Search extends CI_Controller{ 
    function index(){ 

    $search_item = $this->input->post('search_box'); // this is from the input field 

    redirect(base_url()."search/q/".url_title($search_item)); 

    } 



// i've redirected it to the "search" controller then : 

    function q($search_item = null){// process search 
    // load the view here with the data 
    } 

    } 
12
function _remap($parameter){ 

     $this->_index($parameter); 

} 

給它一個嘗試,並告訴我們如何去。

+5

這個工作沒有索引 – foxybagga 2013-09-03 07:07:12

1

這是我的解決方案:

從CI userGuide

public function _remap($method) 
{ 
    if ($method == 'some_method') 
    { 
     $this->$method(); 
    } 
    else 
    { 
     $this->default_method(); 
    } 
} 

所以,我已經建立了我這樣的控制器:

class Shortener extends CI_Controller { 
function shortener() { 
    parent::__construct(); 
} 

function index($getVar) { 
    echo "Get var: " . $getVar; 
} 

function config() { 
    echo "another function"; 
} 

function _remap($method) { 
    if($method == "config") { 
     $this->$method(); 
    } 
    else { 

     $this->index($method); 
    } 
} 
} 

希望這可以幫助別人..

錯誤:如果調用/ shortener _remap函數將「index」傳遞給index()函數...它可以要解決添加if條件或別的東西

16
class Search extends CI_Controller{ 

    function _remap($param) { 
     $this->index($param); 
    } 

    function index($param){ 
     echo $param; 
    } 

} 

然後,您應該能夠訪問爲:/搜索/ 123123:和頁面將回聲出「123123」或任何你把它取代。

+0

的_ infront,它對我來說是完美的,謝謝! – zx1986 2015-11-24 08:13:28

+0

謝謝,它有點隱藏,我忘了在文檔上閱讀它 – 2016-04-30 02:19:10

0

我還必須檢查是否有一個變量被傳遞 - 所以這裏是我的解決方案。

<?php 

defined('BASEPATH') OR exit('No direct script access allowed'); 

class Story extends CI_Controller{ 

    function story() { 
     parent::__construct(); 
    } 

    function _remap($parameter){ 
     $this->_index($parameter); 
    } 

    public function _index($story_id) {   
     if($story_id == 'index'){ 
      //No variable passed 
      redirect('auth', 'refresh'); 
     }else{ 
      $data['title'] = "Story"; 
      $this->load->view('story', $data);     
     } 
    } 

} 

希望這可以幫助別人!

6

如果您的控制器中只有一個稱爲index()的功能,那麼這些解決方案中的大多數僅適用。如果你有這樣的在你的路線:

$route['search/(:any)'] = 'search/index/$1'; 

隨着你的路由上面,會發生什麼是你的搜索功能將工作,但這時如果您添加任何其他功能相同的控制器,他們都會被重定向到/search/index/$1

我能想到的唯一的解決方案允許您使用您想要的URL,同時仍然能夠添加任何其他功能到您的搜索控制器是添加一種凌亂的條件到您的路線文件。下面是它的樣子和它做什麼:

$request = $_SERVER['REQUEST_URI']; // Only add this for readability if(!strpos($request, 'search/another_function') || !strpos($request, 'search/more_functions')) { $route['search/(:any)'] = 'search/index/$1'; }

這是什麼東西做的基本上是說:「如果請求的URL不包含我的任何搜索控制器功能,那麼它必須意味的名字對於索引,我們將激活索引的路由規則「。

這將允許您在沒有問題的情況下接收search/any_other_functions_you_have的請求,並且只在請求URI與它們中的任何一個不匹配時才激活用於隱藏索引函數的規則。

這樣做的一個副作用就是你永遠不會得到404。例如,如果有人輸入一個像「yourdomain.com/search/something」這樣的URL,並且他們希望它顯示一個非搜索結果頁面,將不會得到一個404提醒他們這樣的事實,即沒有這樣的頁面,而是應用程序會假設他們輸入的是搜索詞。然而,聽起來這對你來說並不是什麼大問題,而且我也沒有看到一個控制器不可能返回404s是一件可怕的事情。