亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

codeigniter 3 URI 路由 - 從給定的 url 獲取右側

codeigniter 3 URI 路由 - 從給定的 url 獲取右側

PHP
素胚勾勒不出你 2023-04-21 16:29:22
我需要在 Codeigniter 3 中模擬路由,所以我的問題是如何以編程方式從任何 URL 獲取右側?例如,我有一些路線:$route["blog"] = "Main/blog/en";$route["blog/(:any)"] = "Main/blog/en/$1";$route["novosti"] = "Main/blog/sr";$route["novosti/(:any)"] = "Main/blog/sr/$1";$route["contact"] = "Main/contact/en";$route["kontakt"] = "Main/contact/sr";現在我需要一個可以為給定 URL 部分返回右側的函數,如下所示:echo $this->route->item("novosti/petar")然后應該打印 Main/blog/sr/$1 或 Main/blog/sr/petarCodeigniter 中是否有這樣的功能,因為我在文檔中找不到它?更新: 我正在查看整個系統/路由器類,我看到受保護的函數 _parse_routes 正在做類似的事情,所以如果沒有函數可以給我我需要的東西,我將基于這個創建一個。
查看完整描述

3 回答

?
慕容森

TA貢獻1853條經驗 獲得超18個贊

您可以使用以下代碼獲取所需的信息。

$this->router->routes['novosti/(:any)'];


查看完整回答
反對 回復 2023-04-21
?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

用這個

$this->router->routes['blog']

你會得到

Main/blog/en


查看完整回答
反對 回復 2023-04-21
?
滄海一幻覺

TA貢獻1824條經驗 獲得超5個贊

Codeigniter 很簡單,太簡單了...而且因為對我來說這個函數在哪里并不明顯(如果存在的話)我剛剛采用 _parse_routes 將 URL(slug)解析到我可以找到的右側相應的文件就容易多了。


在這里(如果有人遇到與我相同的情況)。


? function parseRoute($uri) {


? ? // Get HTTP verb

? ? $http_verb = isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : 'cli';


? ? // Loop through the route array looking for wildcards

? ? foreach ($this->router->routes as $key => $val) {

? ? ? // Check if route format is using HTTP verbs

? ? ? if (is_array($val)) {

? ? ? ? $val = array_change_key_case($val, CASE_LOWER);

? ? ? ? if (isset($val[$http_verb])) {

? ? ? ? ? $val = $val[$http_verb];

? ? ? ? } else {

? ? ? ? ? continue;

? ? ? ? }

? ? ? }


? ? ? // Convert wildcards to RegEx

? ? ? $key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);


? ? ? // Does the RegEx match?

? ? ? if (preg_match('#^' . $key . '$#', $uri, $matches)) {

? ? ? ? // Are we using callbacks to process back-references?

? ? ? ? if (!is_string($val) && is_callable($val)) {

? ? ? ? ? // Remove the original string from the matches array.

? ? ? ? ? array_shift($matches);


? ? ? ? ? // Execute the callback using the values in matches as its parameters.

? ? ? ? ? $val = call_user_func_array($val, $matches);

? ? ? ? }

? ? ? ? // Are we using the default routing method for back-references?

? ? ? ? elseif (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE) {

? ? ? ? ? $val = preg_replace('#^' . $key . '$#', $val, $uri);

? ? ? ? }


? ? ? ? return $val;

? ? ? }

? ? }


? ? // If we got this far it means we didn't encounter a

? ? // matching route so we'll set the site default route

? ? return null;

? }

現在,這個:


echo parseRoute("novosti/petar")

將產生:


Main/blog/sr/petar

又名:控制器類/該控制器內的函數/語言參數/博客文章


查看完整回答
反對 回復 2023-04-21
  • 3 回答
  • 0 關注
  • 171 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號