2013-10-05 95 views
1

我使用Laravel 4和Angular JS來處理使用RESTful控制器的$ http請求。Laravel RESTful控制器參數

我有一個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請求低於:

GET

//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 
) 

POST

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

getIndex()方法工作得很好,雖然我懷疑我是否使用正確的過程。

有了上面所說的,我有兩個問題:

  1. Request::get()來檢索XHR參數得到正確的方法是什麼?在我的Javascript中添加dataString到URL的正確方式是以REST方式發送參數?

  2. 如何檢索從我的XHR POST發送的JSON對象?我嘗試了幾種方法,包括Request::get()Input::json(),但我沒有運氣。

在此先感謝。

回答

3

您必須使用$input = Input::all()來檢索使用角度$ 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) { 
    // 
}); 

刪除 - 刪除用戶

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

哎非常感謝您的回答。它是允許您在正斜槓之後放入參數的route :: resource嗎? –

+1

是的。例如在銷燬函數中,我使用正斜槓在角度''http http.delete'' – devo

+0

中傳遞用戶標識,這一定會非常方便,非常感謝! –