Laravel RESTful控制器参数 [英] Laravel RESTful controller parameters
问题描述
我正在将Laravel 4与Angular JS结合使用,以使用RESTful控制器处理$ http请求.
I'm using Laravel 4 with Angular JS to handle $http requests using RESTful controllers.
我有一个RESTful控制器UserController
,它具有以下功能:
I have a RESTful controller, UserController
that has the following functions:
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请求如下:
The $http GET and POST requests I am making are below:
获取
//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()
方法可以很好地工作,尽管我不确定我是否使用了正确的过程.
The getIndex()
method works just fine, although I have doubts on whether I am using the correct procedure.
鉴于上述情况,我有两个问题:
With the above mentioned, I have two questions:
-
Request::get()
是从XHR GET检索参数的正确方法吗?将dataString
附加到Javascript中的URL是RESTful方式发送参数的正确方法吗?
Is
Request::get()
the correct way to retrieve parameters from the XHR GET? Is appendingdataString
to the URL in my Javascript the correct way to send in parameters the RESTful way?
如何检索从XHR POST发送的JSON对象?我尝试了几种方法,包括Request::get()
和Input::json()
,但是我没有运气.
How do I retrieve the JSON object sent from my XHR POST? I have tried several methods including Request::get()
and Input::json()
, but I've had no luck.
谢谢.
推荐答案
您必须使用$input = Input::all()
来检索使用角度$ http发送的数据.然后使用$name = $input['name'];
You have to use $input = Input::all()
to retrieve data send using angular $http. Then use like $name = $input['name'];
如果您使用的是更新的Laravel 4,则使用RESTful API的最佳方法是
And if you are using updated Laravel 4, the best way to use RESTful API is,
控制器看起来像这样,
<?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-获取所有用户
GET - Get all users
$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-获取单个用户进行编辑
GET - Get single user for edit
$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-更新单个用户
PUT - Update single user
$http.put(Data.root_path + 'users/'+entry.id, entry).
success(function(data, status, headers, config) {
//
}).
error(function(data, status, headers, config) {
//
});
POST-保存新用户
POST - Save new user
$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) {
//
});
这篇关于Laravel RESTful控制器参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!