问答详情
源自:3-7 文章业务逻辑

数据库读写没问题,response body部分空

求助~!文章操作这部分,_handleArticleList(),使用 return时,response 的 body 部分为空,用print_r()能显示,代码照着视频中的敲的。User部分的注册就没问题,得到的response 的 body中有注册用户信息。谢谢!

getList

```php

public function getList($user_id,$page=1,$size=10){

        if($size > 100) {

            throw new Exception('Page limited 100', ErrorCode::PAGE_LIMIT_EXCEED);

        }

        $sql = 'SELECT * FROM `article` WHERE `user_id`=:user_id LIMIT :limit, :offset';

        $limit = ($page-1)*$size;

        $limit = $limit < 0 ? 0 : $limit;

        $stmt = $this->_db->prepare($sql);

        $stmt->bindValue(':user_id', $user_id);

        $stmt->bindValue(':limit', $limit);

        $stmt->bindValue(':offset', $size);

        $stmt->execute();

        $data = $stmt->fetchAll(PDO::FETCH_ASSOC);

        //print_r($data);

        return $data;

    

    }

```

_handleArticleList()

```php

    private function _handleArticleList(){

        $user = $this->_userLogin($_SERVER['PHP_AUTH_USER'],$_SERVER['PHP_AUTH_PW']);

        $page = isset($_GET['page']) ? $_GET['page'] : 1;

        $size = isset($_GET['size']) ? $_GET['size'] : 10;

        if($size > 100){

            throw new Exception("Page is too large", 400);

        }

        return $this->_article->getList($user['user_id'], $page, $size);

    }

    private function _handleArticleView(){

        

    }

    private function _userLogin($PHP_AUTH_USER, $PHP_AUTH_PW){

        try{

            return $this->_user->login($PHP_AUTH_USER, $PHP_AUTH_PW);

        }catch(Exception $e){

            if(in_array($e->getCode(),

                [

                    ErrorCode::USERNAME_CANNOT_EMPTY,

                    ErrorCode::PASSWORD_CANNOT_EMPTY,

                    ErrorCode::USERNAME_OR_PASSWORD_INVALID

                ])) {

                    throw new Exception($e->getMessage(), 400);

            }

            throw new Exception($e->getMessage(), 500);

        }

    }

```

提问者:枫叶咚咚咚 2019-01-28 04:31

个回答

  • 枫叶咚咚咚
    2019-01-28 04:39:23

    对不起!我提问在错误的章节了,本想提问在3-11的,回来查看文章逻辑章节后,就提问了。希望看到3-11的小伙伴能回来帮看看拿出问题了。。。谢谢!!