簡體   English   中英

kohana 3使用get創建路線

[英]kohana 3 creating routes with get

這與路由有關。 因此,要通過url獲取參數,您基本上會按照設置的路由格式將數據傳遞到url。

這正在處理鏈接。 我創建了路由,將數據傳遞到url中,並使用request方法獲取了要在控制器中使用的參數。 URL::site("site/$color/$size")

如果我通過表單提交構造網址怎么辦? 例如,如果我想創建一個基本的搜索查詢。

通過get方法提交表單時,如何使表單提交看起來像這個search/orange/large而不是這個search.php?color=orange&size=large

根據定義GET方法將提交的信息作為URL參數。 如果您特別希望以site/$color/$size類的URL結尾,則可以使用POST-REDIRECT-GET模式

一個局部示例,來自我的一個站點上的控制器(頁面上有一個名為clear_cache_button的提交按鈕):

public function action_index()
{
    $session = Session::instance();

    $is_post = (Request::current()->post('submit_button') !== NULL);
    $is_clear_cache = (Request::current()->post('clear_cache_button') !== NULL);

    $p = Database::instance()->table_prefix();
    $people = DB::query(Database::SELECT, "
            SELECT *
            FROM `".$p."Tabe`;
        ")->cached(600, $is_clear_cache)->execute()->as_array('RegID');

    if ($is_clear_cache)
    {
        HTTP::redirect(Request::current()->uri());
    }
    ...
    ...
    ...
}

您可以使用路由過濾器 (v3.3)或回調 (3.1、3.2)並手動設置路由參數。

你可以這樣

public function action_index()
  {
    // this will only be executed if you submmitted a form in your page
    if(Arr::get($_POST,'search')){
      $errors = '';

      $data = Arr::extract($_POST,array('color','size'));

      // you can now access data through the $data array:
      // $data['color'], $data['size']

      // perform validations here
      if($data['color']=='') $error = 'Color is required';
      elseif($data['size']=='') $error = 'Size is required';

      if($error==''){
        $this->request->redirect('search/'.$data['color'].'/'.$data['size']);
      }
    }


    // load your search page view here

    echo 'this is the search page';

  }

希望這可以幫助你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM