[精讚] [會員登入]
3197

[CodeIgniter 3] 取得controller和method的方法

CodeIgniter 3 取得controller和method的方法

分享此文連結 //n.sfs.tw/10846

分享連結 [CodeIgniter 3] 取得controller和method的方法@新精讚
(文章歡迎轉載,務必尊重版權註明連結來源)
2019-10-25 12:24:20 最後編修
2017-03-03 09:45:17 By 張○○
 

自動目錄

在codeigniter2 中,可以使用這些方法取的目前的 method 或 controller 名:

  $this->router->fetch_directory().
  $this->router->fetch_class().
  $this->router->fetch_method()

可是在CI3這些方法都作廢,改用新的方法取代

範例 URL:
http://example.com/auth/test/1/2?f=333

$this->router->directory //這個無作用,空字串

$this->router->class  //aurh

$this->router->method  //test

$this->uri->uri_string();  // auth/test/1/2

$this->uri->segment(0); // NULL

$this->uri->segment(1); // auth

$this->uri->segment(2); // test

$this->uri->segment(3); // 1

$this->uri->segment(4); // 2

$this->uri->segment(5); // NULL

$this->input->server("HTTP_HOST");  // example.com

$this->input->server("SERVER_NAME");  // example.com

上面兩個的差異可以參考Ian懶惰蟲的筆記,原則上,應使用HTTP_HOST較宜

$this->input->server("DOCUMENT_ROOT"); // /path/to/your/folder 結尾沒有 '/'

$this->input->server("SCRIPT_FILENAME"); // /path/to/your/folder/index.php
 
$this->input->server("REDIRECT_URL"); // /auth/test/1/2

$this->input->server("QUERY_STRING"); // f=333

$this->input->server("REQUEST_URI"); // /auth/test/1/2?f=333

 $this->input->server("SCRIPT_NAME"); // /index.php

$this->input->server("PATH_INFO");  // /auth/test/1/2

$this->input->server("PHP_SELF"); // /index.php/auth/test/1/2

範例 省略method 的URL:,
http://example.com/auth/

省略了method,在 config/routes.php 中有這樣的設定
$route['default_controller'] = 'index';

預設CI3 router他會去尋找 index這個method

$this->router->class ; //aurh

$this->router->method;  // index

$this->uri->uri_string();  // auth

print $this->uri->segment(1); // auth

print $this->uri->segment(2); // NULL

參考資料

[1] CI3 官網 http://www.codeigniter.com/userguide3/installation/upgrade_300.html

[2] http://stackoverflow.com/questions/14040738/get-controller-folder-name-in-codeigniter


原文 2015-08-19 19:16:02

END

你可能感興趣的文章

[PHP] 判斷文字、數字、文字加數字的方法 幾個PHP數字和文字操作上的小眉角:判斷文字、數字、文字加數字的方法

[PHP] 陣列新增資料及整理 在php陣列加入項目和重新整理陣列的方法

PHP cli 傳入參數 php cli (PHP命令列的傳入參數使用方法

[PHP] 移除陣列或字串中的重覆元素 移除陣列或字串中的重覆元素

[PHP] 字串拆解函數substr 奇異的錯誤(札記) 偶然發現一個 substr函數拆解上的怪異之處

[PHP]解決ksort新增的SORT_NATURAL|SORT_FLAG_CASE方法 php>=5.4中ksort函數多了SORT_NATURAL 和 SORT_FLAG_CASE 旗標,對舊版的PHP中要怎麼辦?

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

[MAC] 截取螢幕畫面的方法 截取螢幕畫面的方法,在MAC中叫作螢幕快照,英文是screenshot

[jQuery] 利用load()來達成ajax的寫法 jQuery中利用load()來達成ajax的寫法,也有人稱他是假的ajax,作法就是..

[jQuery] textarea 的取值和給值 HTML 的 TEXTAREA 標籤若要用 jquery 取值,不能使用 .text() 或 .html() ,使用 .

世紀帝國征服者新版本--被遺忘的帝國 世紀帝國征服者新版本--被遺忘的帝國 世紀二代的征服者是精典遊戲中的精典,aofe更好玩...

精讚的版面變化 ▓此文僅作為舊文的記錄▓ 這篇文章為了紀念改版完成而撰寫。 原本的部落格是民國97年的作品,那時還是用舊有的技術來寫,很