hyperf: 接收参数

一,创建controller

$ php bin/hyperf.php gen:controller ImageController
App\Controller\ImageController created successfully.

配置路由:

Router::addGroup('/image/',function (){
    Router::get('list','App\Controller\ImageController@listimage');
});

二,controller代码:

class ImageController
{

    public function listimage(RequestInterface $request, ResponseInterface $response) {
        //得到所有参数
        $all = $request->all();
        var_dump($all);

        //得到指定参数
        // 存在则返回,不存在则返回 null
        $aid1 = $request->input('aid');
        echo "aid1:".$aid1."\n";
        // 存在则返回,不存在则返回默认值 Hyperf
        $aid2 = $request->input('aid', 5);
        echo "aid2:".$aid2."\n";

        //判断某个参数是否存在?
        if ($request->has('name')) {
            echo "name参数存在,值为:".$request->input('name')."\n";
        } else {
            echo "name参数不存在\n";
        }

        //


        return $response->raw('Hello list!');
    }

    public function index(RequestInterface $request, ResponseInterface $response)
    {
        return $response->raw('Hello Hyperf!');
    }
}

三,测试效果:

访问:

http://192.168.219.6:9501/image/list?cid=5&bid=7

返回:

array(2) {
  ["cid"]=>
  string(1) "5"
  ["bid"]=>
  string(1) "7"
}
aid1:
aid2:5
name参数不存在

 

posted @ 2025-01-25 11:26  刘宏缔的架构森林  阅读(84)  评论(0)    收藏  举报