如何修改CakePHP 2.x中视图中的URL

时间:2015-01-22 12:29:11

标签: url cakephp

看起来很简单,但有些东西我无法弄清楚。我希望有人能帮助我。

我有一个网址,例如http://host/controller/action/argument/named:1/?query1=1。我想添加另一个查询参数,使其看起来像http://host/controller/action/argument1/argument2/named:1/?query1=1&query2=2。事实上,我想通过一些回调或其他东西将 query2 = 2 添加到特定页面上的所有URL。

现有网页网址中的网址可能有也可能没有查询参数。

我该怎么做?

2 个答案:

答案 0 :(得分:1)

示例网址:http://www.example.com/myController/myAction/param1:val1/param2:val2

您可以使用:

$this->redirect(array("controller" => "myController", 
                  "action" => "myAction",
                  "param1" => "val1",
                  "param2" => "val2",
                  $data_can_be_passed_here),
            $status,
            $exit);

希望它对你有所帮助。

答案 1 :(得分:0)

可能我想的太多了,但这是它的出现方式。我把它放在一个UtilityHelper中。

function urlmodify($params = array(), $baseurl = true)  {

  $top_level_1 = array('plugin', 'controller', 'action'); //top level vars
  $top_level_2 = array('pass', 'named'); //top level vars

  //for integrated use
  $top_level = array_merge($top_level_1, $top_level_2); 

  $urlparams = array();
  //get top level vars
  foreach($top_level as $k)  {
    if(in_array($k, $top_level_1))  {
      $urlparams[$k] = $this->request->params[$k];
    }
    if(in_array($k, $top_level_2))  {
      $$k = $this->request->params[$k]; //create $pass & $named
    }
  }
  //get query vars
  if($this->request->query) {
    $urlparams['?'] = $this->request->query;
  }
  //check for custom pass vars
  if(isset($params['pass']))  {
    $pass = array_merge($pass, $params['pass']);
  }
  //pass var has to be in numarical index
  foreach($pass as $v) {
    array_push($urlparams, $v);
  }
  //check for custom named vars
  if(isset($params['named']))  {
    $named = array_merge($named, $params['named']);
  }
  //pass var has to be in key=>value pair
  foreach($named as $k=>$v) {
    $urlparams[$k] = $v;
  }
  //check for custom query vars
  if(isset($params['?'])) {
    $urlparams['?'] = array_merge($urlparams['?'], $params['?']);
  }
  return Router::url($urlparams, $baseurl);
}

}

我有一个网址:http://localhost/project/exlplugin/logs/manage_columns/1/a:1/n:1/?b=1。在某些链接上,我想添加一些特定的参数。这是我打电话时的结果

echo $this->Utility->urlmodify(array('pass'=>array(2), 'named'=>array('m'=>2), '?'=>array('c'=>2)));*

它提供: http://localhost/thecontrolist/spreadsheet/logs/manage_columns/1/2/a:1/n:1/m:2?b=1&c=2

我只想在我的所有商品信息库中添加一个查询参数 deleted = 0 deleted = 1 以获取SoftDelete的内容:)

感谢@ u2460470的答案,但它只是在视图页面上修改(不删除或创建任何内容,只是添加一些参数)当前URL。