1

I'm using Laravel 4 with Angular JS to handle $http requests using RESTful controllers.

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
}

The $http GET and POST requests I am making are below:

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
)

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:

  1. Is Request::get() the correct way to retrieve parameters from the XHR GET? Is appending dataString to the URL in my Javascript the correct way to send in parameters the RESTful way?

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

Thanks in advance.

1 Answer 1

2

You have to use $input = Input::all() to retrieve data send using angular $http. Then use like $name = $input['name'];

And if you are using updated Laravel 4, the best way to use RESTful API is,

The controller looks like this,

<?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
        );
    }

}

In your route,

Route::resource('users', 'UsersController');

In angular script use,

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 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 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 - 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 - Save new user

$http.post(Data.root_path + 'users', entry).
success(function(data, status, headers, config) {
    //
}).
error(function(data, status, headers, config) {
    //
});

DELETE - Delete a user

$http.delete(Data.root_path +'users/'+id)
.success(function(response) { 
    //
})
.error(function(response) {
    //
});
Sign up to request clarification or add additional context in comments.

2 Comments

hey thanks a lot for the answer. is it the route::resource that allows you to put in parameters after the forward slash?
Yes. for example in destroy function, I am passing the user id using forward slash in angular $http.delete

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.