thinkphp 請求資訊

2021-09-02 21:02:46 字數 2011 閱讀 4235

如果要獲取當前的請求資訊,可以使用\think\request類,

除了下文中的

$request = request::instance();

也可以使用助手函式

$request = request();

當然,最方便的還是使用注入請求物件的方式來獲取變數。

例如:獲取url資訊

$request = request::instance();

// 獲取當前網域名稱

echo 'domain: ' . $request->domain() . '

';// 獲取當前入口檔案

echo 'file: ' . $request->basefile() . '

';// 獲取當前url位址 不含網域名稱

echo 'url: ' . $request->url() . '

';// 獲取包含網域名稱的完整url位址

echo 'url with domain: ' . $request->url(true) . '

';// 獲取當前url位址 不含query_string

echo 'url without query: ' . $request->baseurl() . '

';// 獲取url訪問的root位址

echo 'root:' . $request->root() . '

';// 獲取url訪問的root位址

echo 'root with domain: ' . $request->root(true) . '

';// 獲取url位址中的path_info資訊

echo 'pathinfo: ' . $request->pathinfo() . '

';// 獲取url位址中的path_info資訊 不含字尾

echo 'pathinfo: ' . $request->path() . '

';// 獲取url位址中的字尾資訊

echo 'ext: ' . $request->ext() . '

';輸出結果為:

domain:

file: /index.php

url: /index/index/hello.html?name=thinkphp

url with domain: /index/index/hello.html?name=thinkphp

url without query: /index/index/hello.html

root:

root with domain:

pathinfo: index/index/hello.html

pathinfo: index/index/hello

ext: html

設定/獲取 模組/控制器/操作名稱

$request = request::instance();

echo "當前模組名稱是" . $request->module();

echo "當前控制器名稱是" . $request->controller();

echo "當前操作名稱是" . $request->action();

如果當前訪問的位址是 http://servername/index.php/index/hello_world/index

輸出結果為:

當前模組名稱是index

當前控制器名稱是helloworld

當前操作名稱是index

設定模組名稱值需要向module方法中傳入名稱即可,同樣使用於設定控制器名稱和操作名稱

request::instance()->module('module_name');

獲取請求引數

thinkphp5 請求資訊request

如果要獲取當前的請求資訊,可以使用 think request類,request request instance request request instance 獲取當前網域名稱 echo domain request domain 獲取當前入口檔案 echo file request base...

第八周 ThinkPHP5 0請求資訊

如果要獲取當前的請求資訊,可以使用 think request 類,除了下文中 request request instance 也可以使用助手函式 request request 當然,最方便的還是使用注入請求物件的方式來獲取變數。例如 獲取url資訊 request request instan...

Thinkphp 請求和響應

一.request物件獲取方法 1.request 助手函式獲取 2.think request 類獲取 usethink request class index 二.request 物件的使用方法 注request的param方法獲取的值包括post傳輸的值,使用時盡量使用 request pos...