Laravel Api只返回空对象

时间:2017-03-08 13:53:52

标签: php json laravel

我正在尝试从我的Laravel api路由返回一个对象,但返回的所有内容都是一个空数组。

我的模型看起来像这样:

    class MobilePageStats extends Model
    {
        //
        private $score;
        private $mobileFriendly;
        private $numberRobotedResources;
        private $numberTransientFetchFailureResources;
        private $transientFetchFailureUrls;
        private $cms;
        private $ruleResults;

        /**
         * MobilePageStats constructor.
         * @param int $score
         * @param bool $mobileFriendly
         * @param int $numberRobotedResources
         * @param int $numberTransientFetchFailureResources
         * @param array $transientFetchFailureUrls
         * @param string $cms
         * @param array $ruleResults
         */
        public function __construct(
            $score,
            $mobileFriendly,
            $numberRobotedResources,
            $numberTransientFetchFailureResources,
            $transientFetchFailureUrls,
            $cms,
            $ruleResults
        ) {
            $this->score = $score;
            $this->mobileFriendly = $mobileFriendly;
            $this->numberRobotedResources = $numberRobotedResources;
            $this->numberTransientFetchFailureResources = $numberTransientFetchFailureResources;
            $this->transientFetchFailureUrls = $transientFetchFailureUrls;
            $this->cms = $cms;
            $this->ruleResults = $ruleResults;
        }

我也得到了一切。

我使用构造函数在我的控制器中设置所有数据,在此函数中:

public function getData() {
            $cms = "";
            $score = $this->data->ruleGroups->USABILITY->score;
            $mobileFriendly = $this->data->ruleGroups->USABILITY->pass;
            if(isset($this->data->pageStats->numberRobotedResources)){
                $numberRobotedResources = $this->data->pageStats->numberRobotedResources;
            }else{
                $numberRobotedResources = '';
            }
            if(isset($this->data->pageStats->numberTransientFetchFailureResources)){
                $numberTransientFetchFailureResources = $this->data->pageStats->numberTransientFetchFailureResources;
            }else{
                $numberTransientFetchFailureResources = '';
            }
            if(isset($this->data->pageStats->transientFetchFailureUrls)){
                $transientFetchFailureUrls =  $this->data->pageStats->transientFetchFailureUrls;
            }else{
                $transientFetchFailureUrls = '';
            }
            if(isset($this->data->pageStats->cms)){
                $cms = $this->data->pageStats->cms;
                if($cms != 'WORDPRESS' && $cms != 'JOOMLA'){
                    $cms = $this->checkCMS();
                }
            }
            $cvp = $this->getConfigureViewport();
            $fontSizes = $this->getUseLegibleFontSizes();
            $avoidPlugins = $this->getAvoidPlugins();
            $sizeToViewport = $this->getSizeContentToViewport();
            $tapTargets = $this->getSizeTapTargetsAppropriately();
            $ruleResults = [$cvp, $fontSizes, $avoidPlugins, $sizeToViewport, $tapTargets];


            $mobilePageStats = new MobilePageStats($score, $mobileFriendly, $numberRobotedResources,
                $numberTransientFetchFailureResources, $transientFetchFailureUrls, $cms, $ruleResults);

            return $mobilePageStats;

        }

在我的API路线中,我尝试返回这样的模型:

Route::get('/mobilePageSpeed', function(Request $request){
    $data = new PageSpeedMobileController($request->url);
    return response($data->getData());
});

但是,当我提出请求时,我得到的全部是:

<body>
<pre style="word-wrap: break-word; white-space: pre-wrap;">[]</pre>
</body>

为什么对象没有返回?我知道它包含数据,因为我可以打印它。但是不能发送它?

我试过了repsonse() - &gt; json($ data-&gt; getData());和json_encode($ data-&gt; getData()),但他们给我相同的结果?我似乎无法找到有效的解决方案。

那么如何从Laravel Api返回对象?

2 个答案:

答案 0 :(得分:1)

答案是在将每个对象转换为数组后对每个对象进行json_encode。应该工作:)

答案 1 :(得分:0)

使用您的版本覆盖原始模型构造函数,模型不会填充属性也不会启动。

我不知道你想要实现什么,但是如果你从Eloquent \ Model扩展一个类,你就不能覆盖自己的构造函数而不调用原始的,即:

 public function __construct(array $attributes = [])
 {
    parent::__construct($attributes);