php-Laravel RESTful控制器参数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php-Laravel RESTful控制器参数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3621字,纯文字阅读大概需要6分钟。
内容图文
我将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
)
POST
data = {
'username':username,
'email':email,
'password':password
}
$http.post('user', data).success(
//do something
)
尽管我对我是否使用正确的过程有疑问,但getIndex()方法工作得很好.
基于以上所述,我有两个问题:
> Request :: get()是从XHR GET检索参数的正确方法吗?在我的Javascript中将dataString附加到URL上是否是RESTful方式发送参数的正确方法?
>如何检索从XHR POST发送的JSON对象?我尝试了几种方法,包括Request :: get()和Input :: json(),但我没有运气.
提前致谢.
解决方法:
您必须使用$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) {
//
});
内容总结
以上是互联网集市为您收集整理的php-Laravel RESTful控制器参数全部内容,希望文章能够帮你解决php-Laravel RESTful控制器参数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。