[精讚] [會員登入]
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

[CI3] 使用和修改日曆類別 calendar library CI3有一個日曆圖書館 library,可以快速的建立一個日曆,但是想修改讓他更棒

[PHP] UTF8中取出字串中特定的字數 要將字串,例如資料庫取出的TEXT,取出特定的字數

使用Yahoo OAuth2 1/2 使用Yahoo OAuth2來認證我的網站

[phpmyadmin] 設定預設語言(Language) phpmyadmin預設進來是會選擇你瀏覽器預設的語系,改成正體中文

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

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

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

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

Linux shell 的date表示法 linux下SHELL中的date表示法

設計的工作絕不接受比價 拿買陽春麵的價格想買牛肉麵,寧願倒掉也不賣

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