[精讚] [會員登入]
2987

[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>7.3] switch中的 Did you mean to use "continue 2"? 除錯 這近更新 PHP到7.3版以後,出現這樣的錯:targeting switch is equivalent to "break". Did you mean to use "continue 2"?解決方法

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

PHP判斷對方瀏覽器語系 多語系的網頁應該主動偵查瀏覽器的語系,配合使用者跳出合適的語系。

[PHP] 類別的繼承及建構子和解構子 PHP 類別的繼承及建構子和解構子

[PHP8] 使用autoload autoload+ namespace +use 到了php7之後,namespace和use越來越重要,此篇整理autoload和namespace、use的結合使用。

[PHP] 將字串的部分加上遮罩的函式 有些資料會想要隱藏部分的字元,例如身分證號或信用卡號、電話等。我寫了一個函式來完成這個功能。

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

[Freebsd] 使用 ADSL 撥接上網 Freebsd上要使用 ADSL 撥接上網,該如何設定?

[Win7] 燒錄 iso 檔 在Windows7 中內建燒錄程式,可以直接把檔案拉到光碟機裡,再執行燒錄。

一個邏輯的錯誤刪了全部檔案的經驗 今天本來想做一件很簡單的事,但卻足足浪費我多一倍的時間,再加上刪掉我全部的檔案,原因只是因為我自己的邏輯錯誤。

[AS3] 我做的唯一一個Flash As3遊戲UFO INVADSION [AS3] 我做的唯一一個Flash As3遊戲,是第一個也是最後一個,後來就沒再寫as3,不過as3還滿好玩的。

[AS3] FLASH 引入外部as檔 FLASH AS3中,若要引用另外撰寫的 class(*.as) 檔案,該如何處理?