11<?php
2+ declare (strict_types=1 );
23/**
34 * Created by PhpStorm.
45 * User: chenchangqin
89
910namespace App \Controller ;
1011
11- use Hyperf \HttpServer \Annotation \AutoController ;
12+ use App \Service \UserService ;
13+ use Hyperf \Di \Annotation \Inject ;
14+ use Hyperf \HttpServer \Annotation \Controller ;
15+ use Hyperf \HttpServer \Annotation \RequestMapping ;
1216use Hyperf \HttpServer \Contract \RequestInterface ;
1317
1418/**
15- * @AutoController ()
19+ * @Controller ()
1620 */
1721class UserController extends AbstractController
1822{
23+
24+ /**
25+ * @Inject
26+ * @var UserService
27+ */
28+ private $ userService ;
29+
30+ /**
31+ * @param RequestInterface $request
32+ * @return string
33+ * @RequestMapping(path="index",methods={"get"})
34+ */
1935 public function index (RequestInterface $ request )
2036 {
2137 $ id = $ request ->getMethod ();
2238 return $ id ;
2339 }
2440
25- public function info (RequestInterface $ request )
41+ /**
42+ * @param RequestInterface $request
43+ * @param int $id
44+ * @return array
45+ * @RequestMapping(path="/user/{id:\d+}",methods={"get"})
46+ */
47+ public function info (RequestInterface $ request ,int $ id )
2648 {
27- $ id = $ request ->getMethod ();
28- return $ id ;
49+ $ result = $ this ->userService ->user ($ id );
50+
51+ $ method = $ request ->getMethod ();
52+ return [
53+ 'method ' => $ method ,
54+ 'result ' => $ result
55+ ];
2956 }
57+
58+ /**
59+ * @param RequestInterface $request
60+ * @param int $id
61+ * @return array
62+ * @RequestMapping(path="update/[{id:\d+}]",methods={"put"})
63+ */
64+ public function update (RequestInterface $ request ,int $ id )
65+ {
66+ $ data = $ request ->post ();
67+ $ result = $ this ->userService ->updateUser ($ id ,$ data );
68+
69+ $ method = $ request ->getMethod ();
70+ return [
71+ 'method ' => $ method ,
72+ 'result ' => $ result
73+ ];
74+ }
75+
3076}
0 commit comments