[精讚] [會員登入]
3443

[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] COOKIE的使用 PHP CodeIgniter 3 中COOKIE的使用超簡單

[Centos 6& 7] 安裝php-geoip php-geoip是PHP由domain、ip查詢城市國家資料非常強的函式

設定Google analytics API #2 -- PHP的程式安裝和撰寫 讓你的網站能夠存取你的Google analytics上面的資料

[CodeIgniter 3] 自寫找不到頁面(page404)的方法 使用CI3框架中如果找不到頁面,就會導到一個自定的404頁面,該怎麼做?

[PHP] 2個程式的觀念 整理2個PHP的程式觀念,參考參考。

PHP程式經驗 #2 -- print和echo的差異 常在寫php的人一定會想知道echo和print這兩個函數有什麼不一樣 驗證 1. 比較print 和echo 函式的執

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

SELinux 常用指令和檔案 在Redhat系列中,Centos5以後加入了selinux,他並沒有這麼可怕,不必每次看到Selinux ,就想把他

詭異的創業思維 創業的思維中,有多少銀彈,有多少技術,有多少人脈,有多少時間等等,每個都要考慮進去,以熱忱建立的關係脆弱的像蘇打餅乾一樣..

[AS3] 變數型態 基本類型宣告 as3 有下列幾種基本類型:string, int, number, object, boolean, n

[Mysql/MariaDB] 查看資料庫所占空間 查看資料庫在磁碟中所占空間

安裝git centos/freebsd/windows安裝git