了解Laravel 5 Dingo / Api使用AngularJs的API响应;什么是正确的JSON格式

时间:2015-10-27 08:12:13

标签: php angularjs api laravel dingo-api

我正在使用AngularJs App作为API使用者为Laravel 5 Web应用程序构建API。

除了从AngularJS调用时从API返回的响应外,一切都运行正常。

以下是我在AngularJs App中使用Satellizer的内容

var app = angular
    .module('app', [
        'ngResource',
        'ui.bootstrap',
        'dialogs.main',
        'ui.router',
        'satellizer',
        'ui.router.stateHelper',
        'templates'
    ]);


app.config(['$httpProvider', '$locationProvider', '$stateProvider', '$urlRouterProvider', 'modalStateProvider', '$authProvider',
    function($httpProvider, $locationProvider, $stateProvider, $urlRouterProvider, modalStateProvider, $authProvider)
    {

        var modalInstance,
            modalExit = function() {
                if (modalInstance) {
                    //alert('modalInstance exit');
                    modalInstance.close();
                }
            };

        // Satellizer configuration that specifies which API
        // route the JWT should be retrieved from
        $authProvider.loginUrl = '/api/authenticate';

        $httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';


 $stateProvider
            .state('profile',{
                url: '/profile',
                views: {
                    'contentFullRow': {
                        templateUrl: 'ng/templates/profile/partials/profile-heading-one.html',
                        controller: function($scope, profile){
                            $scope.profile  = profile;
                        }
                    },
                    'contentLeft': {
                        templateUrl: 'ng/templates/profile/partials/profile-body-one.html',
                        controller: function($scope, profile){
                            $scope.profile = profile;
                        }
                    },
                    'sidebarRight': {
                        templateUrl: 'ng/templates/profile/partials/todo-list-one.html',
                        controller: function($scope, profile){
                            $scope.profile = profile;
                        }
                    }
                },
                resolve: {
                    profile: function($http){
                        return $http.get('/api/profile').then(function(data){
                            //This is the issue, I am doing this because of the response returned
                            return data.data.profile;
                        });


                    }
                }
            });


        if(window.history && window.history.pushState){
            $locationProvider.html5Mode({
                enabled: true,
                requireBase: false
            });
        };

    }]);

在我的Laravel控制器

<?php namespace App\Http\Controllers\Profile;

use App\Http\Controllers\Controller;
use App\Models\Profile;

class ProfileController extends Controller
{
     public function __construct()
     {
         $this->middleware('api.auth');
     }

    public function getIndex(){

        $user = $this->auth->user();

        return Profile::find($user->id);
    }
}

来自Laravel的回复 enter image description here

我所面临的挑战是上面的回应。

正如您在Angular Ui路由器的resolve方法中看到的那样, 要从返回的JSON中获取配置文件对象,我必须这样做:

return $http.get('/api/profile').then(function(data){
   return data.data.profile;
}); 

如何让API仅返回没有configheader和其他对象发送的配置文件对象?他们真的有必要吗?我想简单地这样做:

return $http.get('/api/profile').then(function(data){
   return data; //which contains only profile object
}); 

编辑: 我认为我的问题是;这是来自Dingo Api的正确的JSON响应格式吗?

{
  "config",
  "data": {
    "id": 1001,
    "name": "Wing"
  },
  "headers",
  "status",
  "statusText"
}

1 个答案:

答案 0 :(得分:0)

您是否尝试从控制器返回响应,而不是Eloquent对象:

http://laravel.com/docs/master/responses

您可以指定您需要的内容(例如个人资料)。