繁体   English   中英

如何获取细分网址cakephp 3

[英]How to get a segment URL cakephp 3

嗨,我在查看URL片段CAkephp3时遇到了麻烦。 我想从当前URL获取ID。

可以说我的URL是http:// localhost / admin / financial_agreements / edit / 50 ,我想简单地重定向到http:// localhost / admin / financial_agreements / do_print / 50

var urlPrint = "<?=$this->Url->build(['controller' => 'financial_agreements', 'action' => 'do_print', 'I NEED ID FROM CURRENT'], true)?>";

我尝试调试

<?=debug($this->Url->build()); die();?>

但它产生:admin / financial_agreements / edit / 50

50里叫什么? 我的“ url-> build” urlPrint中需要50

对不起,英语不好。

任何帮助将不胜感激。 谢谢。

您可以使用Request对象在视图内获取请求数据(包括url参数)。

尝试以下方法:

$this->request->getParam('pass') //CakePHP 3.4+

$this->request->params['pass'] // CakePHP 3.3

这将返回在URL的动作名称之后传递的所有未命名参数的数组。 例如: /mycontroller/myaction/param1/param2 因此,在此示例中, $this->request->getParam('pass')将产生一个数组,例如: [0 => 'param1', 1 => 'param2']

额外的答案:您还可以在URL中“命名”参数,例如: /mycontroller/myaction/some_name:some_value 要检索这种命名参数,您可以使用相同的技巧,但要使用: $this->request->getParam('named') (使用参数“ named”而不是“ pass”)。

更多信息: https : //book.cakephp.org/3.0/en/controllers/request-response.html https://book.cakephp.org/3.0/en/development/routing.html#passed-arguments

假设您的编辑功能遵循标准惯例,您将获得以下内容:

public function edit($id) {
    $financialAgreement = $this->FinancialAgreements->get($id);
    ...
    $this->set(compact('financialAgreement'));
}

然后在edit.ctp ,您可以非常简单地以$financialAgreement->id获取当前记录的$financialAgreement->id ,因此将使用

$this->Url->build(['controller' => 'financial_agreements', 'action' => 'do_print', $financialAgreement->id], true)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM