0

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.?

Zbarcea Christian
  • 8,320
  • 16
  • 74
  • 124
  • 1
    You should follow the example. It's a great guideline. `/users/index` or `/users` should list all the users, get request for `/users/create` should return all info required to make a user. For example if you have user groups, countries the user can choose from you should return them. post to `/users/create` creates the user etc. [Here](http://codeplanet.io/principles-good-restful-api-design/) is an article I stumbled on that gives great insight into how your API should look like to make it easy for the clients. – Pawel Bieszczad Jun 02 '15 at 20:42
  • 1
    This might be useful. http://stackoverflow.com/questions/671118/what-exactly-is-restful-programming I think the documentation link pretty well sums up how that relates specifically to Laravel. – user1669496 Jun 02 '15 at 21:10

0 Answers0