簡體   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