函數(shù)名稱:Yaf_Request_Simple::get()
適用版本:Yaf 2.2.9及以上版本
函數(shù)說明:Yaf_Request_Simple::get()方法用于獲取HTTP GET請求參數(shù)。
用法:
mixed Yaf_Request_Simple::get(string $name, mixed $default = NULL);
參數(shù):
- $name(必需):要獲取的GET參數(shù)的名稱。
- $default(可選):如果指定的GET參數(shù)不存在,則返回默認值。
返回值:
- 如果指定的GET參數(shù)存在,則返回對應的值。
- 如果指定的GET參數(shù)不存在且未提供默認值,則返回NULL。
- 如果指定的GET參數(shù)不存在但提供了默認值,則返回默認值。
示例: 假設當前URL為:http://example.com/?name=John&age=25
$request = new Yaf_Request_Simple();
$name = $request->get('name');
echo $name; // 輸出:John
$age = $request->get('age');
echo $age; // 輸出:25
$gender = $request->get('gender', 'unknown');
echo $gender; // 輸出:unknown,因為gender參數(shù)不存在并提供了默認值
注意事項:
- Yaf_Request_Simple::get()方法僅適用于Yaf擴展中的Yaf_Request_Simple類。
- 如果你正在使用MVC模式(即Yaf_Request_Http類),則應使用Yaf_Request_Http::getQuery()方法來獲取GET參數(shù)。
- 在Yaf框架中,建議使用Yaf_Request_Abstract類的getParam()方法來獲取請求參數(shù),該方法可以同時獲取GET和POST參數(shù)。