簡體   English   中英

如何在Codeigniter中使用current_url生成新的URL

[英]How to generate a new URL using current_url in codeigniter

我正在嘗試在codeigniter中使用current_url()函數,但不確定如何操作此數據。 我知道我可以回顯current_url(),但是如何獲取每個細分並用於生成新的URL?

我正在嘗試使用當前頁面的各個部分,並通過添加新的變量數據來生成新的URL。

我從未使用current_url(); 之前,所以不確定該怎么做。 假設我的用戶在過濾器頁面上,並且有2個變量可過濾其結果(鄰居和類別)。 如果用戶單擊鄰居,我將如何構建超鏈接以提取當前類別的url變量,然后為鄰居插入變量?

我的網址看起來像:mydomain.com/ny/find/$neighborhood/$category

   Breakdown:
   ny = controller
   find = function
   param1 = $neighborhood
   param2 = $category

任何幫助將非常感激...

您可以通過以下方式細分URL:

For ny: echo $this->uri->segment(1);
For find: echo $this->uri->segment(2);
For param1: echo $this->uri->segment(3);
For param2: echo $this->uri->segment(4);
<?php //check if there is a value in uri segment 3 (param1)

if($this->uri->segment(3)){ 

//if there is a value there then include it in the link ?>
     <a href="controller/function/<?=$this->uri->segment(3)?>/link">Link text</a>

<?php //otherwise just go to the link for param1

}else{ ?>

   <a href="controller/function/link">Link text</a>

<?php } ?>

盡管您可能需要根據確切的邏輯來調整鏈接以及else語句中的邏輯,但這仍然可以工作。 根據您要鏈接的方式,如果用戶位於具有4個URL段的頁面上,則可能還必須添加elseif($this->uri->segment(4))並使用不同的邏輯。

current_url()返回實際加載頁面的字符串。 您可以使用細分創建任意網址。 假設您擁有當前網址:

mydomain.com/ny/find/$neighborhood/$category

如果要向此網址添加內容並創建指向用戶的鏈接,則可以創建一個字符串:

$url = base_url().$this->uri->segment(1).'/'.$this->uri->segment(2).'/newParamYouAdd/'.$this->uri->segment(4);

接着:

<a href="<?php echo $url; ?>">New url</a>

讓我知道這是否是您想要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM