将url从当前页面传递给cakephp中的控制器

时间:2013-07-05 06:44:33

标签: php cakephp cakephp-2.0

我尝试将网址从当前页面传递给控制器​​,但它没有显示我的完整网址。

EX,http://xxxxxx.localhost/cards/card_list/page:2当我在控制器中回显它时显示xxxxxx.localhost。我知道因为("/" ":" ) ......等特殊字符。

我的项目目的是在控制器完成工作后将表单传递给控制器​​,我将其重定向到当前页面。

card_list(查看)

<?php echo $url5 = urlencode(Router::url($this->here, true)); ?>
<?php echo $form->create('CardSaleAll', array('url'=> array('controller' => 'cards', 'action' => 'complete_sale_card', $url5)));?>
<input type="hidden" name="data[CardSaleAll][sale_card_id]" value="'+id+'"> 
<?php echo $form->submit('Submit', array('alt' => '売却','type' => 'image', 'src' => '../../img/btn_52.png', 'style' => 'width: 307px;'));?>

卡(控制器)=&gt; complete_sale_all(action)

function complete_sale_card($url5){
    echo $url5;
    exit();
}

2 个答案:

答案 0 :(得分:1)

尝试以下内容

$this->request->url

以上内容应包含URL作为当前视图请求的一部分。

答案 1 :(得分:0)

使用贪婪的星路线

通常,正斜杠表示参数分隔符:

即。网址/controller/action/one/two/three/four映射到以下变量:

function action ($one, $two, $three, $four) {
}

如果您希望$one包含操作名称后面的所有内容(在上面的示例中),您可以使用greedy-star路由:

Router::connect(
    '/anything/**',
    array('controller' => 'cards', 'action' => 'complete_sale_card')
);

或简化生活

您可以将其设为表单字段,而不是在网址中添加网址:

<?php 
echo $form->create('CardSaleAll', array(
    'action' => 'complete_sale_card'
));
echo $form->input('redirect', array(
    'type'=> 'hidden',
    'value' => $url5
));
?>

不要将原始html与表单助手

混合使用

在问题中有这个片段:

<input type="hidden" name="data[CardSaleAll][sale_card_id]" value="'+id+'"> 

除了是一个错误(除非id确实是一个常数),用手写输入而不是让蛋糕为你生成它是一个坏主意。如果没有别的,因为它很容易生成无效标记或允许注入攻击。而是替换为:

echo $this->Form->input('sale_card_id', array('type' => 'hidden'));

或类似。

不要使用相对网址

在问题中还有这个:

  

'src'=&gt; '../../img/btn_52.png','style'=&gt; 'width:307px;'

这也是一个坏主意。

这适用于网址http://xxxxxx.localhost/cards/card_list/(我假设)并且网址为http://xxxxxx.localhost/cards/card_list(没有尾随斜杠)。

相反:

  1. 在适当的地方使用css - 这是一个适当的例子
  2. 如果您确实想在源代码中添加网址,请使用Router::url()生成正确的绝对网址。