[精讚] [會員登入]
2696

[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

你可能感興趣的文章

[CodeIgniter 3] 資料庫的使用方法整理2/2 CI3 承襲 CI2,有很多的builder class可以用,依各人的使用習慣,有人喜歡一堆sql字串,有人喜歡用helper

[PHP] 移除檔案的UTF8 BOM 移除檔案的UTF8 BOM

設定Google analytics API #1 -- Google網站上的設定 讓你的網站能夠存取你的Google analytics上面的資料

PHP 移除陣列中的元素 要移除陣列中的其中一項元素

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

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

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

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

[Freebsd] 定時測試 ADSL 是否斷線並重連 中華電信 ADSL 雖有固定 ip,可是他卻會不定時「斷線」, 使用以下的 方法可以定時測試是否斷線,以及重新撥接。

[bc] linux 的計算機 bc 設定小數位數、計算π、次方根 linux 的計算機 bc 設定小數位數、計算π、次方根

維修冰箱 維修冰箱

超扯童話血多-賣火柴小女孩 這個真的是太扯了,扯到一直笑,尤其是作者出來那段 因為他第一到第九會連播,笑滿累的。如果你看不懂的話表示你的大腦還滿正經