What is exactly a RESTful API?
I'm working on a school project, and the project name is: ...using a Web RESTful API...
So, I decided to write an Android application as client-side, and for server-side Laravel Framework (version 5).
What I understand when I hear REST API is: get, put, delete, edit, update, index.
This is why I have created a class named APIController
where the project contains all it's methods to communicate with the "exterior".
An example of how I think that user registration should work:
routes.php
Route::post('/register', array('uses' => 'APIController@postRegisterUser', 'as' => 'postRegisterUser'));
And the APIController :
class APIController extends Controller {
public function postRegisterUser() {
$validator = Validator::make(Input::all(), array(
'email' => 'required|email|unique:users,email',
'uuid' => 'required|unique:users,uuid',
'name' => 'required|min:6'
));
if ($validator->fails()) {
return Utils::buildJSONResponse(false, array('Invalid parameters', $validator->messages()), null);
}
$password = Utils::generateRandomString(6);
$result = array(
'email' => Input::get('email'),
'password' => $password,
'uuid' => Input::get('uuid')
);
$user = new User();
$user->email = Input::get('email');
$user->password = Hash::make($password);
$user->name = Input::get('name');
$phone_number = Input::get('phone_number');
if ($phone_number != null) {
$user->phone_number = $phone_number;
}
if (!$user->save()) {
return Utils::buildJSONResponse(false, array('An error occurred'), null);
}
return $response = Utils::buildJSONResponse(true, null, $result);
}
}
And a helper class:
...
public static function buildJSONResponse($success, $errors, $data) {
$result = array();
$result['success'] = $success;
$result['error'] = $errors;
$result['data'] = $data;
return Response::json($result, 200, array(), JSON_PRETTY_PRINT);
}
...
Is this the right approach?
Is this how a REST API should work?
I'm confused, because I have seen other examples on the internet, where people said that the model class (in my case User.php) should contain the REST methods.
Also, I have found this example: http://laravel.com/docs/5.0/controllers#restful-resource-controllers
If you look at the table, you will see that the controller class "PhotoController" comes default with the REST methods. Is this some of abstraction? Should I implement, the methods for creating, editing, deleting, etc.?
链接地址: http://www.djcxy.com/p/71304.html下一篇: 什么是RESTful API?