【问题标题】:Laravel RESTful controller parametersLaravel RESTful 控制器参数
【发布时间】:2013-10-12 08:40:51
【问题描述】:

我正在使用 Laravel 4 和 Angular JS 来使用 RESTful 控制器处理 $http 请求。

我有一个 RESTful 控制器 UserController,它具有以下功能:

public function getIndex(){
    //is Request::get() the correct way to get the parameter?
    echo json_encode(array(
      'username'=>User::countUsername(Request::get('name')),
      'email'=>User::countEmail(Request::get('email'))
    ));
}

public function postIndex(){
    //don't know how to get parameter
}

我发出的 $http GET 和 POST 请求如下:

获取

//is this url the correct way to send in my parameters for GET request?
dataString = 'name='+username+'&email='+email;
$http.get('user?'+dataString).success(
    //do something with returned json
)

发布

data = {
   'username':username,
   'email':email,
   'password':password
}
$http.post('user', data).success(
    //do something
)

getIndex() 方法工作得很好,虽然我怀疑我是否使用了正确的程序。

综上所述,我有两个问题:

  1. Request::get() 是从 XHR GET 检索参数的正确方法吗?在我的 Javascript 中将 dataString 附加到 URL 是否是以 RESTful 方式发送参数的正确方式?

  2. 如何检索从我的 XHR POST 发送的 JSON 对象?我尝试了几种方法,包括Request::get()Input::json(),但都没有运气。

提前致谢。

【问题讨论】:

    标签: php rest xmlhttprequest laravel


    【解决方案1】:

    您必须使用 $input = Input::all() 来检索使用 angular $http 发送的数据。然后使用$name = $input['name'];

    如果您使用的是更新后的 Laravel 4,那么使用 RESTful API 的最佳方式是,

    控制器长这样,

    <?php
    
    
    class UsersController extends BaseController {
    
        /**
         * Display all users.
         *
         * @return Response
         * GET http://localhost/laravel/users
         */
    
        public function index() {
            $users = User::all();
            return $users;
            //return View::make('users.index')->with('users', $users);
        }
    
        /**
         * Show the form for creating a new resource.
         *
         * @return Response
         */
    
        public function create() {
            //
        }
    
        /**
         * Store a newly created resource in storage.
         *
         * @return Response
         * POST http://localhost/laravel/users
         */
    
        public function store() {
            //
        }
    
        /**
         * Display the specified resource.
         *
         * @param  int  $id
         * @return Response
         * GET http://localhost/laravel/users/1
         */
    
        public function show($id) {
            //
        }
    
        /**
         * Show the form for editing the specified resource.
         *
         * @param  int  $id
         * @return Response
         */
    
        public function edit($id) {
            //
        }
    
        /**
         * Update the specified resource in storage.
         *
         * @param  int  $id
         * @return Response
         * PUT http://localhost/laravel/users/1
         */
    
        public function update($id) {
            //
        }
    
        /**
         * Remove the specified resource from storage.
         *
         * @param  int  $id
         * @return Response
         * DELETE http://localhost/laravel/users/1
         */
    
        public function destroy($id) {
            $user = User::find($id);
    
            $user->delete();
    
            return Response::json(array(
                'error' => false,
                'message' => 'User Deleted'),
                200
            );
        }
    
    }
    

    在你的路线中,

    Route::resource('users', 'UsersController');
    

    在角脚本使用中,

    var app = angular.module('myApp', []);
    // include this in php page to define root path
    app.factory('Data', function(){
        return {
            root_path: "<?php echo Request::root(); ?>/"
        };
    });
    

    GET - 获取所有用户

    $http({method: 'GET', url: Data.root_path + 'users'}).
    success(function(data, status, headers, config) {
        $scope.users = data.users;
    }).
    error(function(data, status, headers, config) {
        $scope.users = [];
    });
    

    GET - 获取单个用户进行编辑

    $http({method: 'GET', url: Data.root_path + 'users/'+id}).
    success(function(data, status, headers, config) {
        $scope.entry = data.users[0];
    }).
    error(function(data, status, headers, config) {
        $scope.entry = [];
    });
    

    PUT - 更新单个用户

    $http.put(Data.root_path + 'users/'+entry.id, entry).
    success(function(data, status, headers, config) {
        //
    }).
    error(function(data, status, headers, config) {
        //
    });
    

    POST - 保存新用户

    $http.post(Data.root_path + 'users', entry).
    success(function(data, status, headers, config) {
        //
    }).
    error(function(data, status, headers, config) {
        //
    });
    

    DELETE - 删除用户

    $http.delete(Data.root_path +'users/'+id)
    .success(function(response) { 
        //
    })
    .error(function(response) {
        //
    });
    

    【讨论】:

    • 嘿,非常感谢您的回答。是允许您在正斜杠之后放入参数的 route::resource 吗?
    • 是的。例如在销毁函数中,我使用正斜杠以角度 $http.delete 传递用户 ID
    猜你喜欢
    • 2013-11-14
    • 2016-09-03
    • 2014-09-03
    • 1970-01-01
    • 2014-01-24
    • 2013-08-26
    • 2012-12-24
    • 2015-01-15
    • 1970-01-01
    相关资源
    最近更新 更多