控制器 app/Controller/IndexController.php
<?php
namespace App\Controller;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Contract\RequestInterface;
/**
* @AutoController();
*/
class IndexController
{
public function index(RequestInterface $request){
SetCookie('cookie_name','cookie:huyongjian');
//獲取參數
$name = $request->input('name','huyongjian');
//獲取請求路徑
$uri = $request->path();
//獲取url
$url = $request->url();
// 帶上查詢參數
$fullUrl = $request->fullUrl();
//獲取請求方法
$method = $request->getMethod();
//獲取cookie
$cookieName = $request->cookie('name', 'Hyperf');
return [
'name'=>$name,
'uri'=>$uri,
'url'=>$url,
'fullUrl'=>$fullUrl,
'method'=>$method,
'cookieName'=>$cookieName
];
}
}
測試
curl 118.195.173.53:9501/index/index?name=huyongjian
結果
{
"name": "huyongjian",
"uri": "index\/index",
"url": "http:\/\/118.195.173.53:9501\/index\/index",
"fullUrl": "http:\/\/118.195.173.53:9501\/index\/index?name=huyongjian",
"method": "GET",
"cookieName": "Hyperf"
}
