【问题标题】:kohana 3 creating routes with getkohana 3 使用 get 创建路线
【发布时间】:2013-03-28 03:34:17
【问题描述】:

这与路由有关。所以通过url获取参数,基本上就是按照自己设置的路由格式将数据传给url。

这适用于链接。我创建了路由,将数据传递到url中,并使用请求方法获取参数以供控制器使用。喜欢URL::site("site/$color/$size")

如果我通过表单提交来构建 url 怎么办?例如,如果我想创建一个基本的搜索查询。

当我通过 get 方法提交表单时,如何让我的表单提交看起来像这样 search/orange/large 而不是像这样 search.php?color=orange&size=large。

【问题讨论】:

  • 当从我更喜欢使用Route::get()->uri())的路由创建链接时,在这个例子中这可能看起来像Route::get('fruits')->uri(array('color'=>$color,'size'=>$size))。这种方法的优点是,当路由更改时,链接会自动更改(在您的版本中,您必须在更改路由后手动更改每个链接)

标签: php kohana


【解决方案1】:

definitionGET method将提交的信息作为URL参数。如果您特别想以site/$color/$size 之类的网址结尾,则可以使用POST-REDIRECT-GET pattern。

部分示例,来自我的一个网站上的控制器(页面上有一个名为 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());
    }
    ...
    ...
    ...
}

【讨论】:

    【解决方案2】:

    您可以使用Route filters (v3.3) 或callbacks (3.1, 3.2) 并手动设置路由参数。

    【讨论】:

      【解决方案3】:

      你可以这样做......

      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';
      
        }
      

      希望对你有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多